string path = @"c:\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
//创建一个文件
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
// This text is always added, making the file longer over time
// if it is not deleted.
//向文件追加
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine("This");
sw.WriteLine("is Extra");
sw.WriteLine("Text");
}
先添加 using System.IO;
using (StreamWriter sw =File.AppendText())
{
sw.WriteLine("");//第一行
}
等下一次再用这句话,就会写到第二行
appendtext 是没有就会创建的,不需要创建了
new streamwriter(路径,true);