
这里写目录标题
一级目录
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();
[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();
}
}
}
结果
