代码如下 | 复制代码 |
public class SimpleFileCopy{ static void Main() { string fileName = "test.txt"; string sourcePath = @"C:UsersPublicTestFolder"; string targetPath = @"C:UsersPublicTestFolderSubDir"; string sourceFile = System.IO.Path.Combine(sourcePath, fileName); string destFile = System.IO.Path.Combine(targetPath, fileName); if (!System.IO.Directory.Exists(targetPath)) { System.IO.File.Copy(sourceFile, destFile, true); if (System.IO.Directory.Exists(sourcePath)){ Console.WriteLine("Press any key to exit."); |
下面的示例演示如何移动文件和目录。
代码如下 | 复制代码 |
public class SimpleFileMove { static void Main() string sourceFile = @"C:UsersPublicpublictest.txt"; string destinationFile = @"C:UsersPublicprivatetest.txt"; System.IO.File.Move(sourceFile, destinationFile); System.IO.Directory.Move(@"C:UsersPublicpublictest", @"C:UsersPublicprivate"); |
下面的示例演示如何删除文件和目录。
代码如下 | 复制代码 |
C# public class SimpleFileDelete { static void Main() { if(System.IO.File.Exists(@"C:UsersPublicDeleteTesttest.txt")) try { catch (System.IO.IOException e)
System.IO.FileInfo fi = new System.IO.FileInfo(@"C:UsersPublicDeleteTesttest2.txt"); try { fi.Delete();
try { if(System.IO.Directory.Exists(@"C:UsersPublicDeleteTest")) System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:UsersPublicpublic"); try { |