当前位置: 移动技术网 > IT编程>开发语言>c# > C# 输出字符串到文本文件中的实现代码

C# 输出字符串到文本文件中的实现代码

2019年07月18日  | 移动技术网IT编程  | 我要评论
下面给大家分享一小段代码给大家介绍c# 输出字符串到文本文件中,具体代码如下所示: public class writehelper {

下面给大家分享一小段代码给大家介绍c# 输出字符串到文本文件中,具体代码如下所示:

  public class writehelper
  {
    public static void writefile(object data)
    {
      try
      {
        string path = $@"d:\tokenlog\day{datetime.now:yyyy-mm-dd}";
        var filename = $"tokenlog{datetime.now:yyyy-mm-dd hh}.txt";
        if (!directory.exists(path))
          directory.createdirectory(path);
        textwriter tw = new streamwriter(path.combine(path, filename), true); //true在文件末尾添加数据
        tw.writeline($"----产生时间:{datetime.now:yyyy-mm-dd hh:mm:ss}---------------------------------------------------------------------");
        tw.writeline(data.tojsonstr());
        tw.close();
      }
      catch (exception e)
      {
      }
    }
  }
public static class json
{
    /// <summary>
    /// 转成json字符串
    /// </summary>
    public static string tojsonstr(this object obj)
    {
      return jsonconvert.serializeobject(obj, new jsonserializersettings { referenceloophandling = referenceloophandling.ignore });
    }
}

总结

以上所述是小编给大家介绍的c# 输出字符串到文本文件中的实现代码,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网