一个创建TXT文档并将数据保存到该文档中的方法
public void CreateTxt(string str)//str指要保存的数据的字符串形式
{
if (!File.Exists(@"..\..\数据\" + DateTime.Now.ToString("yyyyMMdd") + ".txt"))//判断以当天日期为命名的TXT文档是不是存在
{
FileStream fs = new FileStream(@"..\..\数据\" + DateTime.Now.ToString("yyyyMMdd") + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));//通过指定字符编码方式可以实现对汉字的支持,否则在用记事本打开查看会出现乱码
sw.Flush();
sw.BaseStream.Seek(0, SeekOrigin.Begin);
sw.WriteLine(str + "\r\n");
sw.Flush();
sw.Close();
}
else
{
string path = @"..\..\数据\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
//System.IO.File.Create(path );
//string ss = "open";
System.IO.File.AppendAllText(path, str + "\r\n", System.Text.Encoding.GetEncoding("GB2312"));
}
}