• Net文件递归查找并保存


    原理:遍历当前文件夹的子文件,保存遍历文件夹下的所有文件
    主要方法(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     }
  • 相关阅读:
    zoj 1002 Fire Net 碉堡的最大数量【DFS】
    hdu 2553 n皇后问题【DFS递归解法】
    UVa11988 Broken Keyboard 损坏的键盘【list】
    hdu 1263 水果 【二维map】
    UVa-156 Ananagrams 反片语【map】【vector】
    hdu 2364 Escape【模拟优先队列】【bfs】
    UVA136 Ugly Numbers【set】【优先队列】
    hdu1429 胜利大逃亡(续) 【BFS】+【状态压缩】
    广搜bfs
    DP:Corn Fields(POJ 3254)
  • 原文地址:https://www.cnblogs.com/ywkcode/p/10992148.html
Copyright © 2020-2023  润新知