代码:
1 string path = Server.MapPath("/Log/Log.txt"); 2 if (File.Exists(path)) 3 { 4 using (StreamWriter sw = File.AppendText(path)) 5 { 6 sw.WriteLine("文本内容"); 7 sw.Flush(); 8 sw.Close(); 9 } 10 } 11 else 12 { 13 using (StreamWriter sw = File.CreateText(path)) 14 { 15 sw.WriteLine("文本内容"); 16 sw.Flush(); 17 sw.Close(); 18 } 19 }
本文介绍了一种使用C#进行文件日志记录的方法。通过判断日志文件是否存在,使用StreamWriter将文本内容追加到指定路径下的Log.txt中,若文件不存在则创建并写入。该方法适用于需要在应用程序中添加日志功能的场景。
&spm=1001.2101.3001.5002&articleId=101733320&d=1&t=3&u=566efa73af344d1ca51bf9fab4c268ee)
607

被折叠的 条评论
为什么被折叠?



