c#編程指南—檔案的複製,移動,刪除

來源:互聯網
上載者:User

 以下樣本說明如何使用 System.IO 命名空間中的 System.IO.File、System.IO.Directory、System.IO.FileInfo 和 System.IO.DirectoryInfo 類以同步方式複製、移動和刪除檔案和檔案夾。這些樣本沒有提供進度列或其他任何使用者介面。如果您想提供一個標準進度對話方塊,請參見如何:提供檔案操作進度對話方塊(C# 編程指南)。

在操作多個檔案時,請使用 System.IO.FileSystemWatcher 提供一些事件,以便可以利用這些事件計算進度。另一種方法是使用平台叫用來調用 Windows Shell 中相應的檔案相關方法。有關如何非同步執行這些檔案操作的資訊,請參見非同步檔案 I/O。

樣本
--------------------------------------------------------------------------------下面的樣本示範如何複製檔案和目錄。Code:

  1. // Simple synchronous file copy operations with no user interface.  
  2. // To run this sample, first create the following directories and files:  
  3. // C:/Users/Public/TestFolder  
  4. // C:/Users/Public/TestFolder/test.txt  
  5. // C:/Users/Public/TestFolder/SubDir/test.txt  
  6. public class SimpleFileCopy  
  7. {  
  8.     static void Main()  
  9.     {  
  10.         string fileName = "test.txt";  
  11.         string sourcePath = @"C:/Users/Public/TestFolder";  
  12.         string targetPath =  @"C:/Users/Public/TestFolder/SubDir";  
  13.   
  14.         // Use Path class to manipulate file and directory paths.  
  15.         string sourceFile = System.IO.Path.Combine(sourcePath, fileName);  
  16.         string destFile = System.IO.Path.Combine(targetPath, fileName);  
  17.   
  18.         // To copy a folder's contents to a new location:  
  19.         // Create a new target folder, if necessary.  
  20.         if (!System.IO.Directory.Exists(targetPath))  
  21.         {  
  22.             System.IO.Directory.CreateDirectory(targetPath);  
  23.         }  
  24.   
  25.         // To copy a file to another location and   
  26.         // overwrite the destination file if it already exists.  
  27.         System.IO.File.Copy(sourceFile, destFile, true);  
  28.   
  29.         // To copy all the files in one directory to another directory.  
  30.         // Get the files in the source folder. (To recursively iterate through  
  31.         // all subfolders under the current directory, see  
  32.         // "How to: Iterate Through a Directory Tree.")  
  33.         // Note: Check for target path was performed previously  
  34.         //       in this code example.  
  35.         if (System.IO.Directory.Exists(sourcePath))  
  36.         {  
  37.             string[] files = System.IO.Directory.GetFiles(sourcePath);  
  38.   
  39.             // Copy the files and overwrite destination files if they already exist.  
  40.             foreach (string s in files)  
  41.             {  
  42.                 // Use static Path methods to extract only the file name from the path.  
  43.                 fileName = System.IO.Path.GetFileName(s);  
  44.                 destFile = System.IO.Path.Combine(targetPath, fileName);  
  45.                 System.IO.File.Copy(s, destFile, true);  
  46.             }  
  47.         }  
  48.         else  
  49.         {  
  50.             Console.WriteLine("Source path does not exist!");  
  51.         }  
  52.   
  53.         // Keep console window open in debug mode.  
  54.         Console.WriteLine("Press any key to exit.");  
  55.         Console.ReadKey();  
  56.     }  
  57. }  
下面的樣本示範如何移動檔案和目錄。Code:
  1. // Simple synchronous file move operations with no user interface.  
  2. public class SimpleFileMove  
  3. {  
  4.     static void Main()  
  5.     {  
  6.         string sourceFile = @"C:/Users/Public/public/test.txt";  
  7.         string destinationFile = @"C:/Users/Public/private/test.txt";  
  8.   
  9.         // To move a file or folder to a new location:  
  10.         System.IO.File.Move(sourceFile, destinationFile);  
  11.   
  12.         // To move an entire directory. To programmatically modify or combine  
  13.         // path strings, use the System.IO.Path class.  
  14.         System.IO.Directory.Move(@"C:/Users/Public/public/test/", @"C:/Users/Public/private");  
  15.     }  
  16. }  

下面的樣本示範如何刪除檔案和目錄。

Code:
  1. // Simple synchronous file deletion operations with no user interface.  
  2. // To run this sample, create the following files on your drive:  
  3. // C:/Users/Public/DeleteTest/test1.txt  
  4. // C:/Users/Public/DeleteTest/test2.txt  
  5. // C:/Users/Public/DeleteTest/SubDir/test2.txt  
  6.   
  7. public class SimpleFileDelete  
  8. {  
  9.     static void Main()  
  10.     {  
  11.         // Delete a file by using File class static method...  
  12.         if(System.IO.File.Exists(@"C:/Users/Public/DeleteTest/test.txt"))  
  13.         {  
  14.             // Use a try block to catch IOExceptions, to  
  15.             // handle the case of the file already being  
  16.             // opened by another process.  
  17.             try  
  18.             {  
  19.                 System.IO.File.Delete(@"C:/Users/Public/DeleteTest/test.txt");  
  20.             }  
  21.             catch (System.IO.IOException e)  
  22.             {  
  23.                 Console.WriteLine(e.Message);  
  24.                 return;  
  25.             }  
  26.         }  
  27.   
  28.         // ...or by using FileInfo instance method.  
  29.         System.IO.FileInfo fi = new System.IO.FileInfo(@"C:/Users/Public/DeleteTest/test2.txt");  
  30.         try  
  31.         {  
  32.             fi.Delete();  
  33.         }  
  34.         catch (System.IO.IOException e)  
  35.         {  
  36.             Console.WriteLine(e.Message);  
  37.         }  
  38.   
  39.         // Delete a directory. Must be writable or empty.  
  40.         try  
  41.         {  
  42.             System.IO.Directory.Delete(@"C:/Users/Public/DeleteTest");  
  43.         }  
  44.         catch (System.IO.IOException e)  
  45.         {  
  46.             Console.WriteLine(e.Message);  
  47.         }  
  48.         // Delete a directory and all subdirectories with Directory static method...  
  49.         if(System.IO.Directory.Exists(@"C:/Users/Public/DeleteTest"))  
  50.         {  
  51.             try  
  52.             {  
  53.                 System.IO.Directory.Delete(@"C:/Users/Public/DeleteTest", true);  
  54.             }  
  55.   
  56.             catch (System.IO.IOException e)  
  57.             {  
  58.                 Console.WriteLine(e.Message);  
  59.             }  
  60.         }  
  61.   
  62.         // ...or with DirectoryInfo instance method.  
  63.         System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:/Users/Public/public");  
  64.         // Delete this dir and all subdirs.  
  65.         try  
  66.         {  
  67.  di.Delete(true);  
  68.         }  
  69.         catch (System.IO.IOException e)  
  70.         {  
  71.             Console.WriteLine(e.Message);  
  72.         }  
  73.   
  74.     }  
  75. }  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.