[英]FileNotFoundException While trying to copy the file from one directory to another
我在“ C:\\ ABC \\ Temp”中有一些文件(.txt,word,excel文件),並且我想將所有.txt文件都移到“ C:\\ ABC \\ Text”中,但是我得到了FileNotFoundException
。
請找到隨附的代碼。
static void Main()
{
string sp = @"C:/ABC/Temp";
string dp = @"C:/ABC/TextFiles";
string[] fileList=Directory.GetFiles(sp);
foreach(string file in fileList)
{
if (file.EndsWith(".txt"))
{
File.Move(sp,dp);
}
}
}
}
}
您正在嘗試使用File.Move
移動整個目錄。
您還必須指定文件名:
File.Move(file, Path.Combine(dp, Path.GetFileName(file)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.