C# 无法将文件移动到其他文件夹。错误返回错误路径
以下是我在移动excel文件中的代码,具体如下C# 无法将文件移动到其他文件夹。错误返回错误路径,c#,path,windows-services,C#,Path,Windows Services,以下是我在移动excel文件中的代码,具体如下 if (Directory.GetFiles(destinationPath, "*.xls").Length != 0) { //Move files to history folder string[] files = Directory.GetFiles(destinationPath); //value -- D://FS//
if (Directory.GetFiles(destinationPath, "*.xls").Length != 0)
{
//Move files to history folder
string[] files = Directory.GetFiles(destinationPath); //value -- D://FS//
foreach (string s in files)
{
var fName = Path.GetFileName(s); //12232015.xls
var sourcePath = Path.Combine(destinationPath, fName);
var destFile = Path.Combine(historyPath, fName); // -- D://FS//History
File.Move(fName, destFile);
}
}
但它得到的错误是
找不到文件“D:\Project\ProjectService\bin\Debug\12232015.xls”。
为什么它在我的项目下找到,而不是在我设置的特定文件夹上
谢谢。有一个逻辑错误。改变
File.Move(fName, destFile);
到
as
fName
仅包含文件名,不包含完整路径。将在工作目录中检查该文件。您仅使用该文件的名称:
var fName = Path.GetFileName(s); //12232015.xls
//...
File.Move(fName, destFile);
如果没有完整的路径,系统将在当前工作目录中查找。这是应用程序正在执行的目录
您应该使用源文件的整个路径:
File.Move(sourcePath, destFile);
明确指定完整路径几乎总是最好的方法。众所周知,相对路径很难管理。Aha!非常感谢你!
File.Move(sourcePath, destFile);