以下示例说明如何使用 System.IO 命名空间中的 System.IO.File、System.IO.Directory、System.IO.FileInfo 和 System.IO.DirectoryInfo 类以同步方式复制、移动和删除文件和文件夹。
下面的示例演示如何移动文件和目录。
// Simple synchronous file move operations with no user interface.public class SimpleFileMove{static void Main(){string sourceFile = @"C:\Users\Public\public\test.txt";string destinationFile = @"C:\Users\Public\private\test.txt";// To move a file or folder to a new location:System.IO.File.Move(sourceFile, destinationFile);// To move an entire directory. To programmatically modify or combine// path strings, use the System.IO.Path class.System.IO.Directory.Move(@"C:\Users\Public\public\test\", @"C:\Users\Public\private");}}
下面的示例演示如何删除文件和目录。
// Simple synchronous file deletion operations with no user interface.// To run this sample, create the following files on your drive:// C:\Users\Public\DeleteTest\test1.txt// C:\Users\Public\DeleteTest\test2.txt// C:\Users\Public\DeleteTest\SubDir\test2.txtpublic class SimpleFileDelete{static void Main(){// Delete a file by using File class static method...if(System.IO.File.Exists(@"C:\Users\Public\DeleteTest\test.txt")){// Use a try block to catch IOExceptions, to// handle the case of the file already being// opened by another process.try{System.IO.File.Delete(@"C:\Users\Public\DeleteTest\test.txt");}catch (System.IO.IOException e){Console.WriteLine(e.Message);return;}}// ...or by using FileInfo instance method.System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\Users\Public\DeleteTest\test2.txt");try{fi.Delete();}catch (System.IO.IOException e){Console.WriteLine(e.Message);}// Delete a directory. Must be writable or empty.try{System.IO.Directory.Delete(@"C:\Users\Public\DeleteTest");}catch (System.IO.IOException e){Console.WriteLine(e.Message);}// Delete a directory and all subdirectories with Directory static method...if(System.IO.Directory.Exists(@"C:\Users\Public\DeleteTest")){try{System.IO.Directory.Delete(@"C:\Users\Public\DeleteTest", true);}catch (System.IO.IOException e){Console.WriteLine(e.Message);}}// ...or with DirectoryInfo instance method.System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\Users\Public\public");// Delete this dir and all subdirs.try{di.Delete(true);}catch (System.IO.IOException e){Console.WriteLine(e.Message);}}}
联系客服