如下:
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(); } }