1

使用File.Copy(src, dest)从包含空格的 UNC 路径复制文件似乎不起作用。据我了解, File.Copy 调用应该适用于带空格的本地路径。我怎样才能使这项工作(没有使用外壳来执行“复制”命令)?

string srcPath1 = @"\\Host\Share\File_name.ext";  
string targetPath1 = @"C:\Temp\target1.file";
File.Copy(srcPath1, targetPath1);                 // OK

string srcPath2 = @"\\Host\Share\File name.ext";  // Note the space
string targetPath2 = @"C:\Temp\target2.file";
File.Copy(srcPath2, targetPath2);                 // File not found

上面示例中的第二个 File.Copy() 会引发 File Not found 异常,声称路径不存在(完整路径,例如,不只是到第一个空格)。

4

1 回答 1

0

如果将路径括在引号中,它会起作用吗?换句话说:

string srcPath2 = String.Concat("\"", @"\\Host\Share\File name.ext", "\"");

于 2013-01-22T13:19:00.563 回答