2012-04-10 111 views
2

這是微軟的代碼示例,用不同的文件位置,這是行不通的:新的C# - 無法獲得File.Copy工作

string fileName = "test1.txt"; 
string sourcePath = @"C:\"; 
string targetPath = @"C:\Test\"; 

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

System.IO.File.Copy(sourceFile, destFile, true); 

它不能找到源文件。如果我把一個破發點,這是我得到:

args {string[0]} string[] 
    fileName "test1.txt" string 
    sourcePath "C:\\" string 
    targetPath "C:\\Test\\" string 
    sourceFile "C:\\test1.txt" string 
    destFile "C:\\Test\\test1.txt" string 

所以它看起來是加倍反斜槓即使使用原義字符串。 (毫無疑問,我在C中有一個test1.txt文件:)任何想法?謝謝!

+3

什麼是異常消息和堆棧跟蹤? – SLaks 2012-04-10 14:13:38

+3

反斜槓加倍僅用於顯示。你收到什麼錯誤信息。 – 2012-04-10 14:13:46

+0

您能否發佈例外文本?雙反斜槓很好。 – Matten 2012-04-10 14:14:21

回答

0

加倍是正確的, 我覺得你的問題是文件名。 嘗試讀取沒有該操作的文件,但在查看是否「隱藏已知類型的擴展名」(如果您的文件名應該是test1.txt.txt)之前,請先閱讀此文件:

+0

謝謝,並多謝了,那就是問題! – MariusD 2012-04-10 14:40:51

3

雙反斜槓是在字符串中表示反斜槓的常用方法。當你使用@你是說你不想解釋任何轉義序列(除其他事項外,請參閱here獲取更多信息,在「文字」下)

所以問題是不同的。是否存在C:\ test1.txt和C:\ Test?你有權限寫入targetPath嗎?

嘗試以下操作(添加異常處理和更多的錯誤檢查需要)

if (!Directory.Exists(targetPath)) { 
    Directory.CreateDirectory(targetPath); 
} 
if (File.Exists(sourceFile)) { 
    File.Copy(sourceFile,destFile,true); 
} 
+1

當然,這是一個評論,而不是一個答案!? – SkonJeet 2012-04-10 14:16:29

+0

它具有與頂級投票答案相同的建議...並更詳細地闡明瞭雙反斜槓 – 2012-04-10 14:17:56

+0

David Heffernan已經涵蓋了所有可能的問題原因 - 您要求OP提供更多信息以便這樣做 - 並且沒有提到這可能是一個訪問問題的可能性。 OP的評論中的其他人已經涵蓋了你在「答案」中的所有內容,但沒有機會獲得代表被標記爲正確的代表。看起來不公平。 – SkonJeet 2012-04-10 14:19:15

5

有3種常見的故障模式:

  1. 源文件C:\test1.txt不存在。
  2. 目標文件C:\Test\test1.txt存在但是隻讀。
  3. 目標目錄C:\Test不存在。

我的猜測是第3項是問題,如果是的話,您需要確保目標目錄存在,然後再致電File.Copy。如果是這種情況,你會看到一個DirectoryNotFoundException。您可以使用Directory.CreateDirectory來確保目標目錄存在。

+0

4. C:\不存在/不可訪問(我不太可能知道) – SkonJeet 2012-04-10 14:15:28

+1

@SkonJeet第1項涵蓋了這種可能性。 – 2012-04-10 14:16:49

+0

True true,+1 .. – SkonJeet 2012-04-10 14:17:28

0

如果您有麻煩嘗試,看看下面這個例子:

using System; 
using System.IO; 
class Test 
{ 
    public static void Main() 
    { 
     string path = @"c:\temp\MyTest.txt"; 
     string path2 = path + "temp"; 

     try 
     { 
      // Create the file and clean up handles. 
      using (FileStream fs = File.Create(path)) {} 

      // Ensure that the target does not exist. 
      File.Delete(path2); 

      // Copy the file. 
      File.Copy(path, path2); 
      Console.WriteLine("{0} copied to {1}", path, path2); 

      // Try to copy the same file again, which should succeed. 
      File.Copy(path, path2, true); 
      Console.WriteLine("The second Copy operation succeeded, which was expected."); 
     } 

     catch 
     { 
      Console.WriteLine("Double copy is not allowed, which was not expected."); 
     } 
    } 
} 

來自反斜線http://msdn.microsoft.com/en-us/library/system.io.file.copy(v=vs.71).aspx

0

要準確查看出了什麼問題,在try-catch塊中的代碼:

try { // Code that can throw an exception } 
catch (Exception ex) 
{ 
    // Show what went wrong 
    // Use Console.Write() if you are using a console 
    MessageBox.Show(ex.Message, "Error!"); 
} 

最有可能的問題是缺少的源文件,目標文件夾不存在,或者您沒有權限訪問該位置。

在Windows 7上,我注意到您需要管理員權限才能直接寫入安裝操作系統的驅動器的根目錄(通常爲c:\)。 嘗試編寫文件或在該位置創建目錄可能會失敗,因此我建議您使用其他位置。