码迷,mamicode.com
首页 > Web开发 > 详细

Net文件递归查找并保存

时间:2019-06-09 00:10:18      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:code   filename   ext   row   exception   cep   cio   name   文件编码   

原理:遍历当前文件夹的子文件,保存遍历文件夹下的所有文件
主要方法(2个):
  //获取文件夹下的所有文件 并保存
  string[] path = Directory.GetFiles(NeedFilePath, "*.*");
  //获取文件夹下的所有子文件
  string[] files = Directory.GetDirectories(NeedFilePath);

代码如下:

 1         /// <summary>
 2         /// 文件递归
 3         /// </summary>
 4         /// <param name="NeedFilePath">需要转化的文件路径</param>
 5         /// <param name="CopyToFilePath">指定生成的文件路径</param>
 6         protected void CreatePath(string NeedFilePath, string CopyToFilePath)
 7         {
 8             string FilePath = CopyToFilePath;
 9             if (!Directory.Exists(FilePath))
10             {
11                 Directory.CreateDirectory(FilePath);
12             }
13             //获取文件夹下的所有文件 并保存
14             string[] path = Directory.GetFiles(NeedFilePath, "*.*");
15             string newfilepath = "";
16             int Count = 0;
17             foreach (string vale in path)
18             {
19                 Count = 0;
20                 Count = vale.Split(\\).Length;
21                 newfilepath = FilePath + "\\" + vale.Split(\\)[Count - 1];
22                 FileHelper.CreateFile(newfilepath);
23                 FileHelper.FileCoppy(vale, newfilepath);
24             }
25             //获取文件夹下的所有子文件
26             string[] files = Directory.GetDirectories(NeedFilePath);
27             foreach (string vale in files)
28             {
29                 Count = vale.Split(\\).Length;
30                 //递归文件夹
31                 CreatePath(vale, CopyToFilePath + "\\" + vale.Split(\\)[Count - 1]);
32             }
33         }

FileHelper类:

  1 public static class FileHelper
  2     {
  3         /// <summary>
  4         /// 写文件
  5         /// </summary>
  6         /// <param name="fileName">文件名</param>
  7         /// <param name="content">文件内容</param>
  8         /// <param name="encoding">指定文件编码</param>
  9         public static void Write_Txt(string fileName, string content, string encoding)
 10         {
 11             if (string.IsNullOrEmpty(fileName))
 12             {
 13                 throw new ArgumentNullException(fileName);
 14             }
 15             if (string.IsNullOrEmpty(content))
 16             {
 17                 throw new ArgumentNullException(content);
 18             }
 19             if (string.IsNullOrEmpty(encoding))
 20             {
 21                 throw new ArgumentNullException(encoding);
 22             }
 23             var code = Encoding.GetEncoding(encoding);
 24             var htmlfilename = HttpContext.Current.Server.MapPath("Precious\\" + fileName  + ".txt");
 25             var str = content;
 26             var sw = StreamWriter.Null;
 27             try
 28             {
 29                 using (sw = new StreamWriter(htmlfilename, false, code))
 30                 {
 31                     sw.Write(str);
 32                     sw.Flush();
 33                 }
 34             }
 35             catch (IOException ioex)
 36             {
 37                 throw new IOException(ioex.Message);
 38             }
 39             catch (Exception ex)
 40             {
 41                 throw new Exception(ex.Message);
 42             }
 43             finally
 44             {
 45                 sw.Close();
 46             }
 47         }
 48         /// <summary>
 49         /// 读文件
 50         /// </summary>
 51         /// <param name="filename">文件路径</param>
 52         /// <param name="encoding">文件编码</param>
 53         /// <returns></returns>
 54         public static string Read_Txt(string filename, string encoding)
 55         {
 56             if (string.IsNullOrEmpty(filename))
 57             {
 58                 throw new ArgumentNullException(filename);
 59             }
 60             if (string.IsNullOrEmpty(encoding))
 61             {
 62                 throw new ArgumentNullException(encoding);
 63             }
 64             var code = Encoding.GetEncoding(encoding);
 65             var temp = HttpContext.Current.Server.MapPath("Precious\\" + filename +  ".txt");
 66             var str = string.Empty;
 67             if (!System.IO.File.Exists(temp)) return str;
 68             var sr = StreamReader.Null;
 69             try
 70             {
 71                 using (sr = new StreamReader(temp, code))
 72                 {
 73                     str = sr.ReadToEnd();
 74                 }
 75             }
 76             catch (IOException ioex)
 77             {
 78                 throw new IOException(ioex.Message);
 79             }
 80             catch (Exception ex)
 81             {
 82                 throw new Exception(ex.Message);
 83             }
 84             finally
 85             {
 86                 sr.Close();
 87             }
 88             return str;
 89         }
 90         /// <summary>
 91         /// 拷贝文件
 92         /// </summary>
 93         /// <param name="orignFile">原始文件</param>
 94         /// <param name="newFile">新文件路径</param>
 95         public static void FileCoppy(string orignFile, string newFile)
 96         {
 97             if (string.IsNullOrEmpty(orignFile))
 98             {
 99                 throw new ArgumentException(orignFile);
100             }
101             if (string.IsNullOrEmpty(newFile))
102             {
103                 throw new ArgumentException(newFile);
104             }
105             System.IO.File.Copy(orignFile, newFile, true);
106         }
107         /// <summary>
108         /// 删除文件
109         /// </summary>
110         /// <param name="path">路径</param>
111         public static void FileDel(string path)
112         {
113             if (string.IsNullOrEmpty(path))
114             {
115                 throw new ArgumentException(path);
116             }
117             System.IO.File.Delete(path);
118         }
119         /// <summary>
120         /// 移动文件
121         /// </summary>
122         /// <param name="orignFile">原始路径</param>
123         /// <param name="newFile">新路径</param>
124         public static void FileMove(string orignFile, string newFile)
125         {
126             if (string.IsNullOrEmpty(orignFile))
127             {
128                 throw new ArgumentException(orignFile);
129             }
130             if (string.IsNullOrEmpty(newFile))
131             {
132                 throw new ArgumentException(newFile);
133             }
134             System.IO.File.Move(orignFile, newFile);
135         }
136         //创建路径
137         public static void CreatePath(string FilePath)
138         {
139             if (!Directory.Exists(FilePath))
140             {
141                 Directory.CreateDirectory(FilePath);
142             }
143         }
144         //创建文件
145         public static void CreateFile(string   FilePath)
146         {
147             if (!File.Exists(FilePath))
148             {
149                 FileStream fs = File.Create(FilePath);
150                 fs.Close();
151             }
152         }
153     }

 

Net文件递归查找并保存

标签:code   filename   ext   row   exception   cep   cio   name   文件编码   

原文地址:https://www.cnblogs.com/ywkcode/p/10992148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!