C#.net winfrom操作txt文件

来源:互联网 发布:淘宝新品如何推广 编辑:程序博客网 时间:2024/05/17 00:05

读取txt

if (File.Exists(System.Windows.Forms.Application.StartupPath + "\\123.txt"))//判断文件是否存在            {                StreamReader sr = new StreamReader(System.Windows.Forms.Application.StartupPath + "\\123.txt", false);                string str = sr.ReadLine().ToString();                sr.Close();//读取                return str;            }            else            {                FileStream fs = new FileStream(System.Windows.Forms.Application.StartupPath + "\\123.txt", FileMode.Create, FileAccess.Write);//创建写入文件                 fs.Close();            }
修改txt

            StreamWriter sw = new StreamWriter(System.Windows.Forms.Application.StartupPath + "\\datetime.txt", false);            sw.WriteLine(newTime);            sw.Close();//写入

此处System.Windows.Forms.Application.StartupPath为winfrom用来判断程序路径的语法

如果是web需要用server.mappath判断。

0 0
原创粉丝点击