2013-02-28 187 views
3

我試圖創建一個目錄和子目錄,並將文件從一個位置複製到另一個位置。下面的代碼有效,但如果有子目錄,它不會創建父目錄(10_new)。我試圖將所有內容(包括子目錄)從"c:\\sourceLoc\\10"複製到"c:\\destLoc\\10_new"文件夾。如果"10_new"不存在,那麼我應該創建這個文件夾。請協助。將文件從一個位置複製到另一個位置

string sourceLoc = "c:\\sourceLoc\\10"; 
string destLoc = "c:\\destLoc\\10_new"; 

foreach (string dirPath in Directory.GetDirectories(sourceLoc, "*", SearchOption.AllDirectories)) 
{ 
    Directory.CreateDirectory(dirPath.Replace(sourceLoc, destLoc)); 
    if (Directory.Exists(sourceLoc)) 
    { 
     //Copy all the files 
     foreach (string newPath in Directory.GetFiles(sourceLoc, "*.*", SearchOption.AllDirectories)) 
      File.Copy(newPath, newPath.Replace(sourceLoc, destLoc)); 
    } 
} 
+0

Idk如果已經有一個簡單的peasy庫函數,但是您可以遞歸地檢查子文件夾並複製每個文件夾。 – Amicable 2013-02-28 16:12:22

+0

請檢查http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c-sharp – 2013-02-28 16:13:33

回答

4

取自看你的代碼,你永遠不會檢查父文件夾的存在。您跳轉到首先獲取所有子文件夾。

if (!Directory.Exists(@"C:\my\dir")) Directory.CreateDirectory(@"C:\my\dir"); 
1

在做File.Copy之前,檢查並確保文件夾存在。如果它不創建它。 該函數將檢查路徑是否存在,如果不存在,它將創建它。如果它無法創建它,由於某種原因,它將返回false。否則,是正確的。

Private Function checkDir(ByVal path As String) As Boolean 
     Dim dir As New DirectoryInfo(path) 
     Dim exist As Boolean = True 
     If Not dir.Exists Then 
      Try 
       dir.Create() 
      Catch ex As Exception 
       exist = False 
      End Try 
     End If 
     Return exist 
    End Function 

請記住,所有的.NET語言編譯下到CLR(公共語言運行時),所以如果這是在VB.Net或C#也不要緊。在兩者之間轉換的一個好方法是:http://converter.telerik.com/

+0

問題是關於C#,而不是VB – 2013-02-28 16:15:37

+0

所有.Net語言都可以編譯到CLR中,並且可以很容易地在各種語言之間進行轉換(http://converter.telerik.com/)。此外,這個問題不是依賴於語言的,他們沒有詢問語法。 – jason 2013-02-28 16:16:52

+0

我同意傑森。不知道你用什麼語言,你應該明白這個代碼旁邊的邏輯是什麼! – Maris 2013-02-28 16:19:53

1

這裏是如何的所有目錄中的文件複製到另一個目錄

這是從http://msdn.microsoft.com/en-us/library/cc148994.aspx

 string sourcePath = "c:\\sourceLoc\\10"; 
     string targetPath = "c:\\destLoc\\10_new"; 
     string fileName = string.Empty; 
     string destFile = string.Empty; 

     // To copy all the files in one directory to another directory. 
     // Get the files in the source folder. (To recursively iterate through 
     // all subfolders under the current directory, see 
     // "How to: Iterate Through a Directory Tree.") 
     // Note: Check for target path was performed previously 
     //  in this code example. 
     if (System.IO.Directory.Exists(sourcePath)) 
     { 
      string[] files = System.IO.Directory.GetFiles(sourcePath); 

      // Copy the files and overwrite destination files if they already exist. 
      foreach (string s in files) 
      { 
       // Use static Path methods to extract only the file name from the path. 
       fileName = System.IO.Path.GetFileName(s); 
       destFile = System.IO.Path.Combine(targetPath, fileName); 
       System.IO.File.Copy(s, destFile, true); 
      } 
     } 
     else 
     { 
      Console.WriteLine("Source path does not exist!"); 
     } 
+0

它不復制子文件夾圖像。 – Billy 2013-10-22 09:51:47

+0

@Billy直接從上面:要遞歸遍歷當前目錄下的所有子文件夾,請參閱「如何:遍歷目錄樹。 – abc123 2013-10-22 18:42:13

-1

這是不可能複製或在Windows 7

移動與C#文件,這反而創造了零個字節的文件。

相關問題