string保存成TXT文件

// <summary>
/// 检查是否存在文件夹
/// </summary>
public void change()
{
string path = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\TextMessage.txt";
if (!Directory.Exists(Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd")))
{
Directory.CreateDirectory(Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd"));
}

if (!File.Exists(path))
{
FileStream fs = File.Create(@"" + DateTime.Now.ToString("yyyy-MM-dd") + "\\TextMessage.txt");
fs.Close();
}

}
/// <summary>
/// 写入文本文件
/// </summary>
/// <param name="value"></param>
public void text(string value)
{

change();
string path = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\TextMessage.txt";
FileStream f = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(f);
sw.WriteLine(value);
sw.Flush();
sw.Close();
f.Close();
}
posted @ 2015-06-21 23:10  huayhh  阅读(971)  评论(0编辑  收藏  举报