简体   繁体   中英

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. 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. If you have permission try a proper file share UNC path:

\\\\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. Alternatively create a specific share as per the answer by Markus.

From 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.

"\\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.

Alternatively use the default drive share: eg \\10.10.10.48\\C$\\QBpluginlink.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM