为什么会出现“系统找不到指定文件”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Cinch;
using System.Windows.Forms;
using System.Windows.Threading;
namespace PIC
{
public class VM:ViewModelBase
{
/// <summary>
/// 路径
/// </summary>
public string FolderPath { get; private set; }
//public IList<string> imageList{get;set;}
public string ImagePath { get; set; }
private int index = 0;
//private string path = System.Environment.CurrentDirectory;
DispatcherTimer timer = new DispatcherTimer();
/// <summary>
/// 图片文件名
/// </summary>
public IList<string> ImageFileList { get; private set; }
public SimpleCommand ChangeFolderCommand { get; private set; }
public SimpleCommand PreviousImageCommand { get; private set; }
public SimpleCommand NextImageCommand { get; private set; }
public SimpleCommand ClockWiseCommand { get; private set; }
public SimpleCommand CounterClockWiseCommand { get; private set; }
public SimpleCommand StartSlideShowCommand { get; private set; }
public SimpleCommand StopSlideShowCommand { get; private set; }
public SimpleCommand DeleteImageCommand { get; private set; }
/// <summary>
///
/// </summary>
/// <param name="folderPath"></param>
public VM(string folderPath)
{
this.ImageFileList = new List<string>();
ChangeFolderPath(folderPath);
this.ChangeFolderCommand = new SimpleCommand
{
ExecuteDelegate=x=>ChangeFolder()
};
this.PreviousImageCommand = new SimpleCommand
{
ExecuteDelegate = x => PreviousImage()
};
this.NextImageCommand = new SimpleCommand
{
ExecuteDelegate = x => NextImage()
};
this.StopSlideShowCommand = new SimpleCommand
{
ExecuteDelegate =x=>StopSlideShow()
};
this.DeleteImageCommand = new SimpleCommand
{
ExecuteDelegate=x=>DeleteImage()
};
timer.Interval = TimeSpan.FromMilliseconds(1000);
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
if (index >= ImageFileList.Count)
{
index = 0;
}
ImagePath = ImageFileList[index];
index++;
}
void StopSlideShow()
{
timer.Stop();
}
void ChangeFolder()
{
FolderBrowserDialog dialog=new FolderBrowserDialog();
if(dialog.ShowDialog()==DialogResult.OK)
{
ChangeFolderPath(ImagePath);
ImageFileList.Clear();
//
}
}
void PreviousImage()
{
index--;
if (index < 0)
{
MessageBox.Show("已是第一张");
return;
}
ImagePath=ImageFileList[index];
}
void NextImage()
{
index--;
if (index >ImageFileList.Count-1)
{
MessageBox.Show("已是最后一张");
return;
}
ImagePath = ImageFileList[index];
}
void StartSlideShow()
{
timer.Start();
}
void DeleteImage()
{
if (ImagePath != null)
// if (_deletingImg != null)
{
if (MessageBox.Show("是否删除", "提示", MessageBoxButtons.YesNo) ==
System.Windows.Forms.DialogResult.Yes)
{
try
{
//this.pictureBox.Image.Dispose();
//this.pictureBox.Image = null;
DeleteImage(ImagePath);
}
catch (Exception ex)
{
MessageBox.Show("删除失败", ex.ToString(), MessageBoxButtons.OK);
//throw;
}
finally
{
//this.LoadImage();
}
}
}
else
{
MessageBox.Show("选择图片");
}
}
/// <summary>
/// 验证路径合法性
/// </summary>
/// <param name="folderPath"></param>
void ValidateFilePath(string folderPath)
{
if (Directory.Exists(folderPath) == false)
{
throw new Exception("错误");
}
return;
}
/// <summary>
/// 更改文件路径
/// </summary>
/// <param name="folderPath"></param>
public void ChangeFolderPath(string folderPath)
{
ValidateFilePath(folderPath);
this.FolderPath = folderPath;
ReadImages();
}
/// <summary>
/// 读取文件资源
/// </summary>
void ReadImages()
{
//清空原有的文件路径
//遍历新文件夹路径下的文件
//填充图片文件名
// ImageFiles
string[] imagePath;
ImageFileList.Clear();
try
{
imagePath = Directory.GetFiles(FolderPath, "*.jpg");
for (int i = 0; i < imagePath.Length; i++)
{
ImageFileList.Add(imagePath[i]);
}
}
catch (Exception e)
{
throw new Exception("错误", e);
}
}
void DeleteImage(string imageFileName)
{
if (File.Exists(imageFileName) == true)
{
try
{
File.Delete(imageFileName);
ImageFileList.Remove(imageFileName);
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
}
}
}
VM v = new VM(System.Environment.CurrentDirectory);