如何自动将数据保存到指定txt本上?(c#)

2025-03-04 20:02:34
推荐回答(1个)
回答1:

string strPath = 获取文件所在路径

if (!File.Exists(strPath + "\\log.txt"))
{
FileStream fs = File.Create(strPath + "\\log.txt");
fs.Close();
}

try
{
StreamWriter sw = File.AppendText(strPath + "\\log.txt");

sw.WriteLine("写入文件的内容");
sw.Close();
}
catch(Exception e)
{

}

希望有所帮助