使用file.copy()移动zip文件

ali*_*ce7 2 c#

我正在尝试将文件从服务器移动\\abc\\C$\\temp\\coll.zip到另一台服务器 \\def\\c$\\temp.

我正在尝试使用File.Copy(source,destination).但我收到源路径中的错误说:Couldn't find the part of the path.

我不确定源路径有什么问题.

Blu*_*kMN 5

如果您使用的是C#,请确保转义"\"字符.您必须将反斜杠加倍或使用@作为字符串文字的前缀,如下所示:

string fileName = @"\\abc\C$\temp\coll.zip";

要么

string fileName = "\\\\abc\\C$\\temp\\coll.zip";


Seb*_*son 5

您可以使用C#@ Verbatim并在代码中使用检查,如下所示:

string source = @"\\abc\C$\temp\coll.zip";
string destination = @"\\def\c$\temp\coll.zip";
string destDirectory = Path.GetDirectoryName(destination)
if (File.Exists(source) && Directory.Exists(destDirectory)) {
    File.Copy(source, destination);
}
else {
    // Throw error or alert
}
Run Code Online (Sandbox Code Playgroud)