zoukankan      html  css  js  c++  java
  • c#写入txt文件内容和自动新建txt

    //简洁版
    public static void AddLgoToTXT(string logstring)
    {
      string path = AppDomain.CurrentDomain.BaseDirectory + "operalog.txt";
      if (!System.IO.File.Exists(path))
      {
        FileStream stream = System.IO.File.Create(path);
        stream.Close();
        stream.Dispose();
      }
      using (StreamWriter writer = new StreamWriter(path, true))
      {
        writer.WriteLine(logstring);
      }
    }
    //带自动删除版
    public static void Logtest(string logstring)
    {
      try
      {
        string path = AppDomain.CurrentDomain.BaseDirectory + "operalog.txt";
        //判断文件是否存在,没有则创建。
        if (!System.IO.File.Exists(path))
          {
            FileStream stream = System.IO.File.Create(path);
            stream.Close();
            stream.Dispose();
          }
    
        //写入日志
        using (StreamWriter writer = new StreamWriter(path, true))
        {
          writer.WriteLine(logstring); 
        }
    
        long size = 0;
    
        //获取文件大小
        using (FileStream file = System.IO.File.OpenRead(path))
        {
          size = file.Length;//文件大小。byte
        }
        
        //判断日志文件大于2M,自动删除。
        if (size > (1024 * 4 * 512))
        { 
          System.IO.File.Delete(path);
        }
      }
      catch
      {
    
      }
    }
  • 相关阅读:
    天下大事必作于细,天下难事必作于易
    mybatis 配置 log4j 日志
    org/w3c/dom/ElementTraversal 错误解决办法
    naoting
    FreeMarker 生成Java、mybatis文件
    在mysql数据库中创建oracle scott用户的四个表及插入初始化数据
    音视频编码格式汇总
    java 二进制数字符串转换工具类
    Linux nohup 命令
    Linux & 命令
  • 原文地址:https://www.cnblogs.com/yuyexiaoxiao/p/12009344.html
Copyright © 2011-2022 走看看