文件夹操作

众所周知,C#有Directory类和File类包含了目录和文件的各种方法,我们平时的移动文件夹,复制文件夹,或删除文件夹等,很多都会有文件夹里有文件夹,有时里面还有文件等等。这样移起来就不是那么方便了。自己写了个文件夹的操作类。

  1    /// <summary>
  2     /// C#文件夹操作
  3     /// </summary>
  4     public class DirectoryHelp
  5     {
  6         /// <summary>
  7         /// 拷贝文件夹
  8         /// </summary>
  9         /// <param name="sourceDirPath">源路径</param>
 10         /// <param name="destDirPath">目的路径</param>
 11         public static void Copy(string sourceDirPath, string destDirPath)
 12         {
 13             if (Directory.Exists(sourceDirPath))
 14             {
 15                 if (!Directory.Exists(destDirPath))
 16                     Create(destDirPath);
 17 
 18                 DirectoryInfo di = new DirectoryInfo(sourceDirPath);
 19 
 20                 foreach (FileInfo fi in di.GetFiles())
 21                 {
 22                     string toName = destDirPath + "\\" + fi.Name;
 23                     File.Copy(fi.FullName, toName, true);
 24                 }
 25 
 26                 foreach (DirectoryInfo dir in di.GetDirectories())
 27                 {
 28                     Copy(dir.FullName, destDirPath + dir.FullName.Replace(sourceDirPath, ""));
 29                 }
 30             }
 31         }
 32 
 33         /// <summary>
 34         /// 移动文件夹
 35         /// </summary>
 36         /// <param name="sourceDirPath">源路径</param>
 37         /// <param name="destDirPath">目的路径</param>
 38         public static void Move(string sourceDirPath, string destDirPath)
 39         {
 40             if (Directory.Exists(sourceDirPath))
 41             {
 42                 if (!Directory.Exists(destDirPath))
 43                     Create(destDirPath);
 44 
 45                 DirectoryInfo di = new DirectoryInfo(sourceDirPath);
 46 
 47                 foreach (FileInfo fi in di.GetFiles())
 48                 {
 49                     string toName = destDirPath + "\\" + fi.Name;
 50                     if (File.Exists(toName))
 51                     {
 52                         File.Delete(toName);
 53                     }
 54                     File.Move(fi.FullName, toName);
 55                 }
 56 
 57                 foreach (DirectoryInfo dir in di.GetDirectories())
 58                 {
 59                     Move(dir.FullName, destDirPath + dir.FullName.Replace(sourceDirPath, ""));
 60                 }
 61 
 62                 Directory.Delete(sourceDirPath);
 63             }
 64         }
 65 
 66         /// <summary>
 67         /// 创建文件夹
 68         /// </summary>
 69         /// <param name="dirPath">路径</param>
 70         public static void Create(string dirPath)
 71         {
 72             string[] paths = dirPath.Split('\\');
 73             string path = paths[0];
 74             for (int i = 1; i < paths.Length; i++)
 75             {
 76                 path = path + "\\" + paths[i];
 77                 if (!Directory.Exists(path))
 78                 {
 79                     Directory.CreateDirectory(path);
 80                 }
 81             }
 82         }
 83 
 84         /// <summary>
 85         /// 删除文件夹
 86         /// </summary>
 87         /// <param name="dirPath">路径</param>
 88         public static void Delete(string dirPath)
 89         {
 90             if (Directory.Exists(dirPath))
 91             {
 92                 DirectoryInfo di = new DirectoryInfo(dirPath);
 93                 foreach (FileInfo fi in di.GetFiles())
 94                 {
 95                     fi.Delete();
 96                 }
 97 
 98                 foreach (DirectoryInfo dir in di.GetDirectories())
 99                 {
100                     Delete(dir.FullName);
101                 }
102                 Directory.Delete(dirPath);
103             }
104         }
105     }

 

你可能感兴趣的:(文件夹)