批量替换文件夹里面的文本文件的指定字符

View Code
 1         ///<summary>
2 /// 初始化加载
3 ///</summary>
4 public static void Update()
5 {
6 //文件夹路径
7 string path = @"D:\408";
8 //获取文件夹
9 DirectoryInfo dir = new DirectoryInfo(path);
10 GetFileInfo(path, dir);
11
12 }
13 ///<summary>
14 /// 遍历文件夹下面的子文件
15 ///</summary>
16 ///<param name="pathFile">文件夹路径</param>
17 ///<param name="dir">文件夹名</param>
18 private static void GetFileInfo(string pathFile, DirectoryInfo dir)
19 {
20
21 //获取文件夹下面的子文件
22 FileInfo[] dirInfo = dir.GetFiles();
23 DataTable dt = new DataTable();
24 //遍历子文件
25 foreach (FileInfo info in dirInfo)
26 {
27 //子文件名称
28 string filename = info.Name.ToString();
29 Console.WriteLine(filename);
30 htm(@"D:\408\" + filename);
31
32 }
33
34
35 }
36 ///<summary>
37 /// 替换文件
38 ///</summary>
39 ///<param name="path">文本文件路径</param>
40 public static void htm(string path)
41 {
42 string html = "";
43 using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
44 {
45 using (StreamReader reader = new StreamReader(stream))
46 {
47 html = reader.ReadToEnd().Trim();
48 html = html.Replace("", "D").Replace("", "B").Replace("", "A").Replace("", "C");
49
50
51 }
52 }
53 DataInit(html, path);
54 }
55 ///<summary>
56 /// 保存问题换过的文本文件
57 ///</summary>
58 ///<param name="html">替换过的文件内容</param>
59 ///<param name="path">保存路径</param>
60 public static void DataInit(string html, string path)
61 {
62
63 using (Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write))
64 {
65 using (StreamWriter writer = new StreamWriter(stream))
66 {
67 writer.Write(html);
68 }
69 }
70 }
原文地址:https://www.cnblogs.com/happygx/p/2244869.html