簡體   English   中英

嘗試將文件從我的計算機復制到同一網絡上的另一台計算機

[英]Trying to copy a file from my computer to another computer on the same network

這是我的代碼:

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)
   {
   }
}

當執行此代碼時,出現異常

不支持給定路徑的格式

誰能告訴我我可能在哪里弄錯了?

問題是您要將文件復制到的UNC路徑中的C: :。 您可以將其更改為目標計算機上的有效共享,也可以使用管理共享(如果啟用了這些共享,並且帳戶具有足夠的權限這樣做):

@"\\10.10.10.48\ValidShareName\QBpluginlink.txt", // Valid share name

@"\\10.10.10.48\C$\QBpluginlink.txt", // Administrative share

即使您在Windows資源管理器中嘗試了該路徑,該路徑也不起作用。 如果您有權限,請嘗試使用正確的文件共享UNC路徑:

\\\\10.10.10.148\\c$\\QBpluginlink.txt

請注意c$ ,這是Windows訪問C:驅動器的默認管理共享設置-但是您將需要正確的權限。 或者,根據Markus的答案創建特定的份額。

從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);

請確保您有權將文件復制到服務器

目標必須是有效的文件路徑,在您的情況下,必須是有效的UNC路徑。

“ \\ 10.10.10.48 \\ C:\\ QBpluginlink.txt”無效,因為您正在引用該計算機的c:驅動器,因此需要在目標服務器中創建一個共享文件夾並使用該路徑。

或者使用默認驅動器共享:例如\\ 10.10.10.48 \\ C $ \\ QBpluginlink.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM