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; } }

 

posted @ 2013-09-12 10:57  水目之痕  阅读(269)  评论(0)    收藏  举报