2015-12-02 192 views
1

這裏的是我的代碼:嘗試將文件從我的計算機複製到另一臺計算機在同一網絡

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

當該代碼被執行我得到的異常

給定的路徑的格式不支持

任何人都可以告訴我我可能在哪裏弄錯了嗎?

回答

5

問題是您要將文件複製到的UNC路徑中的C:。要麼你改變這是在目標計算機上的有效共享,或者你使用的管理共享(如果這些被啓用,該帳戶擁有足夠的權限來這樣做):

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

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

你打我給它+1 – CSL

+0

@Markus 有沒有辦法提供用戶名和密碼的路徑? – Mainak

+0

如果您模擬此帳戶,還可以在特定帳戶下運行代碼。但在這種情況下,您有責任以安全的方式存儲憑據。如果您可以先在Windows資源管理器中使用它,則會更容易,例如通過授予應用程序帳戶的訪問權限或指定憑據並將其保存在Windows密碼存儲區中。如果基本訪問已到位,您的應用程序不需要做任何特殊的事情。 – Markus

2

這條道路不會即使工作你在Windows資源管理器中嘗試過。如果您有權限嘗試一個適當的文件共享的UNC路徑:

\\10.10.10.148\c$\QBpluginlink.txt

注意c$,它是由Windows中的默認管理共享設置訪問C:驅動器 - 但您需要正確的權限。或者根據Markus的回答創建一個特定的分享。

0

目的地必須是一個有效的文件路徑,在你的情況下一個有效的UNC路徑。

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

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

1

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

請確保您有訪問複製文件去服務器

相關問題