向txt文件中添加或者追加文字字符串

如下:

   if (!File.Exists(@"D:MyFile.txt"))
            {
                FileStream fs = new FileStream(@"D:MyFile.txt", FileMode.Create, FileAccess.Write);
            }

            else
            {
                using (FileStream fs = new FileStream(@"D:MyFile.txt", FileMode.Append, FileAccess.Write))//追加文字
                {
                    fs.Lock(0, fs.Length);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine("我是最好的,虽然现在不是");
                    sw.WriteLine("有信息,不一定会赢,但没有信心,肯定会输");
                    sw.WriteLine("我的期待月薪为{0,9:C4},实现年龄为{1,9},成功率{2,9:P2}", new Object[] { 20000, 30, 1 });
                    fs.Unlock(0, fs.Length);//一定要用在Flush()方法以前,否则抛出异常。
                    sw.Flush();
                }
            }

参考链接:https://www.cnblogs.com/vaevvaev/p/6804423.html

原文地址:https://www.cnblogs.com/tiancaige/p/12719755.html