C#txt文件写入,若不存在则生成文件,若存在则直接追加写入
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20if (!System.IO.File.Exists(path)) { FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);//创建写入文件 System.IO.File.SetAttributes(path, FileAttributes.ReadOnly); //设置文件属性为只读 StreamWriter sw = new StreamWriter(fs1, Encoding.GetEncoding("utf-8")); sw.WriteLine("写入内容");//开始写入值 sw.Close(); fs1.Close(); } else { new FileInfo(path).Attributes = FileAttributes.Normal;//获取文件的属性进行修改在操作 FileStream fs1 = new FileStream(path, FileMode.Append, FileAccess.Write);//打开文件 StreamWriter sw = new StreamWriter(fs1, Encoding.GetEncoding("utf-8")); sw.WriteLine(error);//开始写入值 sw.Close(); fs1.Close(); }
最后
以上就是精明老鼠最近收集整理的关于C# txt文件写入,若不存在则生成文件,若存在则直接追加写入的全部内容,更多相关C#内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复