1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > C# 读写txt文件 写txt(IO) TXT追加

C# 读写txt文件 写txt(IO) TXT追加

时间:2020-03-28 14:00:06

相关推荐

C# 读写txt文件 写txt(IO) TXT追加

VS-C++ 系列:所有相关C++文章链接.

VS-C# 系列:所有相关C#文章链接.

bat 系列:所有相关bat文章链接.

OpenCV 系列:所有相关OpenCV文章链接.

AD 系列:所有相关文章链接

Keil 系列:所有相关文章链接

Arduino 系列:所有相关Arduino文章链接

Git SVN 系列:所有相关Git SVN文章链接

Linux 系列:所有相关文章链接

python 系列:所有相关文章链接

Raspberry Pi Pico 系列:所有相关文章链接

所有内容均以最小系统调试成功;逐步提供低分源码工程下载

保证每行代码都经过验证!

如有疑惑,欢迎留言,看见即回;祝好__by Dxg_LC

目录:Dxg_C# 开发小技巧整理集合

C# 通过IO 读写txt文件Dxg-原创出品,如需转载,请注明出处;

序言:

1、以上链接为方便整理查看资料用;伴随博文发布更新,如果有不正确处,感谢指正

2、因本人能力有限若有不正确之处或者相关超链接失效,请于相关文章内提醒@博主;灰常感谢

3、友情提醒1,勿要《一支烟 + 一杯茶 == 一坐一下午》 身体重要,革命本钱;

4、友情提醒2,多喝热水;

5、友情提醒3,听媳妇话+多点时间陪家人;

C# 通过IO 读写txt文件

获取当前exe路径的方法:可参考

通过string路径变量获取文件名、扩展名、没有扩展名的文件名

//using:using System.IO;

写入 txt·way1

FileStream fs = new FileStream("D:\\Time.txt", FileMode.Create);//获得字节数组byte[] pu8data = System.Text.Encoding.Default.GetBytes(string.Format("Time:{0}\r\n", DateTime.Now.ToString()));//开始写入fs.Write(pu8data, 0, pu8data.Length);//清空缓冲区、关闭流fs.Flush();fs.Close();

·way2

FileStream fs = new FileStream("D:\\Time.txt", FileMode.Create);StreamWriter sw = new StreamWriter(fs);//开始写入sw.Write(string.Format("Time:{0}\r\n", DateTime.Now.ToString()));//清空缓冲区sw.Flush();//关闭流sw.Close();fs.Close();

·追加文字

using (FileStream fs = new FileStream(@"C:\AddString.txt", FileMode.OpenOrCreate, FileAccess.Write)){using (StreamWriter sw = new StreamWriter(fs)){sw.BaseStream.Seek(0, SeekOrigin.End);sw.WriteLine("{0}\n", msg, DateTime.Now);sw.Flush();}}

·完整例子 代码

string pPath = Application.StartupPath + "\\tmp";if (!Directory.Exists(pPath)){System.IO.Directory.CreateDirectory(pPath);}string IniPath = System.Environment.CurrentDirectory + "\\tmp\\Ayz.txt";if (File.Exists(IniPath))File.Delete(IniPath);FileStream fs = new FileStream(IniPath, FileMode.Create);StreamWriter sw = new StreamWriter(fs);sw.Write(string.Format("{0,8} {1,4} {2,4} {3,4} {4,4} {5,4} {6,4} {7,4} {8,4} {9,4} {10,4} {11,4} {12,4} {13,8} {14,8}\r\n","PicFrame_","R", "G", "B", "R-B", "R-G", "G-B","Lv1", "Lv2", "Lv3", "Lv4", "Lv5", "Lv6","Lv_End", "ZZCD_nm"));for (int i = 0; i < 50; i++){sw.Write(string.Format("{0,8} {1,4} {2,4} {3,4} {4,4} {5,4} {6,4} {7,4} {8,4} {9,4} {10,4} {11,4} {12,4} {13,8} {14,8}\r\n",i, 20, 30, 40, 50, 60, 70, 10.5, 20.5, 30.5, 40.5, 50.5, 60.5, 3, 65.5));}//清空缓冲区sw.Flush();//关闭流sw.Close();fs.Close();

Dxg-原创出品,如需转载,请注明出处;

欢迎收藏,点赞;"一键三联"走起,LOL

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。