2009-12-29 43 views
12

Tôi có một thư mục có 10 tệp văn bản tại C: \ TEXTFILES \ drive trong máy của tôi. Tôi muốn sao chép thư mục TEXTFILES và nội dung của nó hoàn toàn từ máy tính của tôi sang một máy khác. Làm thế nào để sao chép giống nhau bằng cách sử dụng C#.Sao chép thư mục trong C#

+0

Xin hãy giúp tôi với một số mẫu mã. Vì tôi cần sao chép chính thư mục từ máy chủ này sang máy chủ khác – venkat

+0

@sukmar, không có cách nào để sao chép thư mục bằng cách sử dụng một hàm .NET duy nhất, điều này là do bảo mật. Bạn sẽ cần phải viết chức năng của riêng bạn. Xem câu trả lời của tôi để biết chi tiết. – serhio

+0

.NET có thể cung cấp cho chúng tôi phương pháp "cấp cao" này trong khuôn khổ. – Seva

Trả lời

33
using System; 
using System.IO; 

class DirectoryCopyExample 
{ 
    static void Main() 
    { 
     DirectoryCopy(".", @".\temp", true); 
    } 

    private static void DirectoryCopy(
     string sourceDirName, string destDirName, bool copySubDirs) 
    { 
     DirectoryInfo dir = new DirectoryInfo(sourceDirName); 
     DirectoryInfo[] dirs = dir.GetDirectories(); 

     // If the source directory does not exist, throw an exception. 
     if (!dir.Exists) 
     { 
      throw new DirectoryNotFoundException(
       "Source directory does not exist or could not be found: " 
       + sourceDirName); 
     } 

     // If the destination directory does not exist, create it. 
     if (!Directory.Exists(destDirName)) 
     { 
      Directory.CreateDirectory(destDirName); 
     } 


     // Get the file contents of the directory to copy. 
     FileInfo[] files = dir.GetFiles(); 

     foreach (FileInfo file in files) 
     { 
      // Create the path to the new copy of the file. 
      string temppath = Path.Combine(destDirName, file.Name); 

      // Copy the file. 
      file.CopyTo(temppath, false); 
     } 

     // If copySubDirs is true, copy the subdirectories. 
     if (copySubDirs) 
     { 

      foreach (DirectoryInfo subdir in dirs) 
      { 
       // Create the subdirectory. 
       string temppath = Path.Combine(destDirName, subdir.Name); 

       // Copy the subdirectories. 
       DirectoryCopy(subdir.FullName, temppath, copySubDirs); 
      } 
     } 
    } 
} 

Từ MSDN

+0

Điều này sẽ ổn nếu cả nguồn và đích của máy được kết nối. Nhưng mã trên ném ngoại lệ nếu máy đích không được kết nối hoặc có thể yêu cầu thông tin xác thực người dùng kết nối. Sau đó, trong loại kịch bản mã trên bị lỗi. Có cách nào để khắc phục loại ngoại lệ này xảy ra trong quá trình chuyển tập tin từ máy này sang máy khác không. – venkat

+3

Bạn mong đợi kết quả như thế nào nếu máy đích không được kết nối hoặc dự kiến ​​thông tin đăng nhập để hoàn thành hoạt động? Tôi nghĩ rằng một ngoại lệ sẽ là hành vi thích hợp nếu máy đích không có sẵn vì tôi không thể nghĩ ra bất kỳ cách nào để buộc đích đến trực tuyến. Nếu thông tin đăng nhập được yêu cầu thì có lẽ một hộp thoại để yêu cầu thông tin đăng nhập có thể được hiển thị. Giải pháp –

+0

không hoạt động nếu có 2 cấp bậc của Hệ thống phân cấp thư mục. Nó chỉ sao chép nó vào thư mục cơ sở. – whihathac

-4

Bạn sẽ tìm thấy tất cả những gì bạn cần trong không gian tên System.IO và cụ thể là các lớp FileDirectory.

+2

-1 không hữu ích. – toddmo

2
 string path = @"C:\TEXTFILES\"; 
     string path2 = @"P:\myNetworkPath\tesssst"; 

     try 
     { 
      Directory.CreateDirectory(path2); 

      foreach (string fileName in Directory.GetFiles(path)) 
      { 
       File.Copy(
        Path.Combine(path, fileName), 
        Path.Combine(path2, fileName), true); 
      } 
     } 
     catch 
     { 
      Console.WriteLine("Exception"); 
     } 

Đối với một bản sao sâu hơn, xem:

http://www.codeproject.com/KB/files/copydirectoriesrecursive.aspx

9
private void copyDirectory(string strSource, string strDestination) 
{ 
    if (!Directory.Exists(strDestination)) 
    { 
     Directory.CreateDirectory(strDestination); 
    } 

    DirectoryInfo dirInfo = new DirectoryInfo(strSource); 
    FileInfo[] files = dirInfo.GetFiles(); 
    foreach(FileInfo tempfile in files) 
    { 
     tempfile.CopyTo(Path.Combine(strDestination,tempfile.Name)); 
    } 

    DirectoryInfo[] directories = dirInfo.GetDirectories(); 
    foreach(DirectoryInfo tempdir in directories) 
    { 
     copyDirectory(Path.Combine(strSource, tempdir.Name), Path.Combine(strDestination, tempdir.Name)); 
    } 

} 
Các vấn đề liên quan