0
点赞
收藏
分享

微信扫一扫

C#编程:对于TXT文件的写处理


C#编程:对于TXT文件的写处理_应用程序


这里写目录标题

  • ​​一级目录​​
  • ​​2021-5-19​​
  • ​​对于TXT文件的写操作​​
  • ​​结果​​

一级目录

2021-5-19

对于TXT文件的写操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApp2
{

static class Program
{
//加入锁
private readonly static object o = new object();

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
string p = "D://1.txt";
string str = "图控大叔开始写文件了";
string value = $"写入时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ssfff")} 写入内容:{str}";


lock (o)
{
try
{
FileStream fs;
StreamWriter sw;
fs = new FileStream(p, FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write);
sw = new StreamWriter(fs);

sw.WriteLine(value);
sw.Close();
fs.Close();

}
catch (Exception ex)
{
MessageBox.Show($"写文件失败, 原因{ex.Message}", "Err");
}
}

System.Console.ReadKey();
}

}
}

结果

C#编程:对于TXT文件的写处理_txt文件_02



举报

相关推荐

0 条评论