话不多说直接上干货(静态帮助类,亲测可用):
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Base.Common.File.LocalFile { public class TxtOperation { /// <summary> /// 读取txt文件内容,并返回集合(一行为一个数据) /// </summary> /// <param name="Path">文件地址</param> public static List<String> ReadTxtContents(string Path) { StreamReader sr = new StreamReader(Path, Encoding.Default); List<String> contents = new List<string>(); string lineContent = ""; while ((lineContent = sr.ReadLine()) != null) { contents.Add(lineContent); } sr.Close(); return contents; } /// <summary> /// 读取txt文件内容,并返回集合(一行为一个数据) /// </summary> /// <param name="Path">文件地址</param> public static string ReadTxtAllContent(string Path) { StreamReader sr = new StreamReader(Path, Encoding.Default); string content = sr.ReadToEnd(); sr.Close(); return content; } /// <summary> /// 保存文件 /// </summary> /// <param name="savePath"></param> /// <param name="text"></param> public static void CreateTxtWithText(string savePath, string text) { try { System.IO.File.WriteAllText(savePath, text); } catch (Exception ex) { throw; } } /// <summary> /// 保存文件 /// </summary> /// <param name="savePath"></param> /// <param name="text"></param> public static void CreateTxtWithText(string savePath, List<string> texts) { try { System.IO.StreamWriter file = new System.IO.StreamWriter(savePath, true); foreach (var text in texts) { //file.Write(text); file.WriteLine(text); } file.Flush(); file.Close(); } catch (Exception ex) { throw; } } } }
最后
以上就是勤恳小蚂蚁最近收集整理的关于C#Txt文本读取以及创建保存的全部内容,更多相关C#Txt文本读取以及创建保存内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复