/// <summary>
/// 图片文件迁移
/// </summary>
private void MoveFile(string Frompath, string directoryPath)
{
string[] picList = Directory.GetFiles(Frompath, "*.jpg"); //图片
string[] txtList = Directory.GetFiles(Frompath, "*.txt"); //文本文件
string[] pdfList = Directory.GetFiles(Frompath, "*.pdf"); //PDF文件
foreach (string f in picList)
{
//取得文件名.
string fName = f.Substring(Frompath.Length + 1);
File.Copy(Path.Combine(Frompath, fName), Path.Combine(directoryPath, fName), true);
}
foreach (string f in txtList)
{
string fName = f.Substring(Frompath.Length + 1);
try
{
File.Copy(Path.Combine(Frompath, fName), Path.Combine(directoryPath, fName));
}
// 捕捉异常.
catch (IOException copyError)
{
MessageBox.Show(copyError.Message);
}
}
foreach (string f in pdfList)
{
string fName = f.Substring(Frompath.Length + 1);
try
{
File.Copy(System.IO.Path.Combine(Frompath, fName), System.IO.Path.Combine(directoryPath, fName));
}
catch (IOException copyError)
{
MessageBox.Show(copyError.Message);
return;
}
}
//删除原始文件夹里的文件
foreach (string f in txtList)
{
File.Delete(f);
}
foreach (string f in picList)
{
File.Delete(f);
}
foreach (string f in pdfList)
{
File.Delete(f);
}
}
C# 文件迁移
最新推荐文章于 2024-09-04 17:23:08 发布