打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C#实现将日志写入文本文件的方法
userphoto

2017.03.22

关注
using System.IO;
public static void WriteLog(string strLog)
{
  string sFilePath="d:\\"+DateTime.Now.ToString("yyyyMM");
  string sFileName = "rizhi" + DateTime.Now.ToString("dd") + ".log";
  sFileName = sFilePath+ "\\"+sFileName; //文件的绝对路径
  if (!Directory.Exists(sFilePath))//验证路径是否存在
  {
    Directory.CreateDirectory(sFilePath);
    //不存在则创建
  }
  FileStream fs;     
  StreamWriter sw;
  if (File.Exists(sFileName))
  //验证文件是否存在,有则追加,无则创建
  {
    fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
  }
  else
  {
    fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
  }
  sw = new StreamWriter(fs);
  sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "   ---   " + strLog);
  sw.Close();
  fs.Close();   
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
获取数据库表结构和表数据的小程序(VB.NET版本)
日志记录代码
C#学习笔记(22)
C#读写txt文件的两种方法介绍
c#文件操作
API 测试(8)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服