class Log
{
public static void Write(string str)
{
FileStream fs = null;
string LogDir = Directory.GetCurrentDirectory() + "\\log\\";
Directory.CreateDirectory(LogDir);
string fileName = LogDir + DateTime.Now.ToString("yyyyMMdd") + ".txt";
//将待写的入数据从字符串转换为字节数组
Encoding encoder = Encoding.UTF8;
byte[] bytes = encoder.GetBytes(DateTime.Now.ToString("hh:mm:ss") + ": " + str + Environment.NewLine);
try
{
fs = File.OpenWrite(fileName);
//设定书写的開始位置为文件的末尾
fs.Position = fs.Length;
//将待写入内容追加到文件末尾
fs.Write(bytes, 0, bytes.Length);
// fs.Write();
}
catch (Exception ex)
{
Console.WriteLine("文件打开失败{0}", ex.ToString());
}
finally
{
fs.Close();
}
}
}
原文:http://my.oschina.net/u/582827/blog/473109