C# 文件名、目录名或卷标语法不正确,File.Copy
我正在尝试将文件从网络共享文件夹复制到同一网络中的另一个文件夹。它会显示异常情况 已验证,并且文件存在C# 文件名、目录名或卷标语法不正确,File.Copy,c#,file,C#,File,我正在尝试将文件从网络共享文件夹复制到同一网络中的另一个文件夹。它会显示异常情况 已验证,并且文件存在 文件大小为400Kb。源文件和目标文件都应该是文件名。如果destiFilePath实际上是一个目录,那么让我们添加源文件名(ABC_1382.pdf) 抛出了什么异常?目标文件夹是否存在?您需要从目标路径`\servername\folder1\New`中删除最后一个\,目标应该是文件名,例如\servername\folder1\New\ABC\U 1382.pdf@这是问题的标题,是的,
文件大小为400Kb。源文件和目标文件都应该是文件名。如果
destiFilePath
实际上是一个目录,那么让我们添加源文件名(ABC_1382.pdf
)
抛出了什么异常?目标文件夹是否存在?您需要从目标路径`\servername\folder1\New`
中删除最后一个\
,目标
应该是文件名,例如\servername\folder1\New\ABC\U 1382.pdf
@这是问题的标题,是的,文件夹存在当路径是变量时,我如何添加“@”?只是海螺吗?执行“@”+路径无效。你能帮我一下吗?@iamCR:@
用于逐字字符串:我只是不想每个“\”都加倍,即@\\servername\folder1\Old\ABC\u 1382.pdf”=“\\\\\servername\\folder1\\Old\ABC\u 1382.pdf”
。如果使用变量,则不应在必要时使用“@”
,而应使用Path.Combine
,例如sourceFilePath=Path.Combine(服务器、目录)
Source: \\servername\folder1\Old\ABC_1382.pdf
Destination: \\servername\folder1\New\
File.Copy(sourceFilePath, destiFilePath, true);
string sourceFilePath = @"\\servername\folder1\Old\ABC_1382.pdf";
string destiFilePath = @"\\servername\folder1\New\";
// Uncomment, if you are not sure that directory exists
// and you want to create it with all subdirs
// Directory.CreateDirectory(destiFilePath);
File.Copy(sourceFilePath,
Directory.Exists(destiFilePath)
? Path.Combine(destiFilePath, Path.GetFileName(sourceFilePath))
: destiFilePath,
true);