C# 读写txt文件
C# 读写txt文件
class WriterReadLog {
string path = Application.StartupPath + @"\log.txt"; public WriterReadLog() { } public static void Write(string logContent) { string content = System.DateTime.Now.ToString("yyyy-MM-dd hh:MM:ss "); content += logContent;try { FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write); StreamWriter writer = new StreamWriter(fs, Encoding.Default); writer.WriteLine(content); writer.Close(); fs.Close(); } catch (Exception ex) { } } public static string Read(string path) { string content = ""; FileStream fs = null; ; StreamReader reader = null;try { fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite); reader = new StreamReader(fs, Encoding.Default); content = reader.ReadToEnd(); reader.Close(); fs.Close(); } catch (Exception ex) { } return content; } }
种一棵树最好的时间是十年前,其次是现在.