[英]Trying to copy a file from my computer to another computer on the same network
Here is my code: 这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
try
{
File.Copy(@"C:\Documents and Settings\subhayan\Desktop\QBpluginlink.txt", @"\\10.10.10.148\C:\QBpluginlink.txt", true);
}
catch (Exception ex)
{
}
}
when this code is executed I get the exception 当执行此代码时,出现异常
The given path's format is not supported
不支持给定路径的格式
can anyone tell me where I may have mistaken ? 谁能告诉我我可能在哪里弄错了?
The problem is the C:
in the UNC-path that you want to copy the file to. 问题是您要将文件复制到的UNC路径中的
C:
:。 Either you change this to be a valid share on the target computer or you use an administrative share (if these are enabled and the account has sufficient rights to do so): 您可以将其更改为目标计算机上的有效共享,也可以使用管理共享(如果启用了这些共享,并且帐户具有足够的权限这样做):
@"\\10.10.10.48\ValidShareName\QBpluginlink.txt", // Valid share name
@"\\10.10.10.48\C$\QBpluginlink.txt", // Administrative share
That path wouldn't work even if you tried it in windows explorer. 即使您在Windows资源管理器中尝试了该路径,该路径也不起作用。 If you have permission try a proper file share UNC path:
如果您有权限,请尝试使用正确的文件共享UNC路径:
\\\\10.10.10.148\\c$\\QBpluginlink.txt
Note the c$
, it is a default admin share setup by windows to access the C:
drive - but you will need the correct permissions. 请注意
c$
,这是Windows访问C:
驱动器的默认管理共享设置-但是您将需要正确的权限。 Alternatively create a specific share as per the answer by Markus. 或者,根据Markus的答案创建特定的份额。
From MSDN: 从MSDN:
string fileName = @"QBpluginlink.txt";
string sourcePath = @"C:\Documents and Settings\subhayan\Desktop";
string targetPath = @"\\10.10.10.148\C$";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
// To copy a file to another location and
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);
Please make sure you have an access to copy a file going to the server 请确保您有权将文件复制到服务器
The destination has to be a valid file path, in your case a valid UNC path. 目标必须是有效的文件路径,在您的情况下,必须是有效的UNC路径。
"\\10.10.10.48\\C:\\QBpluginlink.txt" is not valid because you are referencing the c: drive of that comoputer, you need to create a shared folder in your destination server and use that path. “ \\ 10.10.10.48 \\ C:\\ QBpluginlink.txt”无效,因为您正在引用该计算机的c:驱动器,因此需要在目标服务器中创建一个共享文件夹并使用该路径。
Alternatively use the default drive share: eg \\10.10.10.48\\C$\\QBpluginlink.txt 或者使用默认驱动器共享:例如\\ 10.10.10.48 \\ C $ \\ QBpluginlink.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.