无法将文件移动到另一个文件夹。路径错误 returns
Cannot move a file to another folder. Error returns on a wrong path
这是我移动 excel 文件的具体代码。
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);
}
}
但是它得到一个错误
Could not find file 'D:\Project\ProjectService\bin\Debug232015.xls'.
为什么它在我的项目下找到,而不是在我设置的特定文件夹下?
谢谢。
存在逻辑错误。变化
File.Move(fName, destFile);
至
File.Move(sourcePath, destFile);
as fName
只包含文件名而不是完整路径。该文件已在工作目录中检查。
您只使用了文件名:
var fName = Path.GetFileName(s); //12232015.xls
//...
File.Move(fName, destFile);
如果没有完整路径,系统将在当前工作目录中查找。应用程序执行的目录。
您应该使用源文件的完整路径:
File.Move(sourcePath, destFile);
明确指定完整路径是几乎总是最好的方法。众所周知,相对路径难以管理。
这是我移动 excel 文件的具体代码。
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);
}
}
但是它得到一个错误
Could not find file 'D:\Project\ProjectService\bin\Debug232015.xls'.
为什么它在我的项目下找到,而不是在我设置的特定文件夹下?
谢谢。
存在逻辑错误。变化
File.Move(fName, destFile);
至
File.Move(sourcePath, destFile);
as fName
只包含文件名而不是完整路径。该文件已在工作目录中检查。
您只使用了文件名:
var fName = Path.GetFileName(s); //12232015.xls
//...
File.Move(fName, destFile);
如果没有完整路径,系统将在当前工作目录中查找。应用程序执行的目录。
您应该使用源文件的完整路径:
File.Move(sourcePath, destFile);
明确指定完整路径是几乎总是最好的方法。众所周知,相对路径难以管理。