C# 如何创建txt文件!谢谢

2024-12-31 14:20:19
推荐回答(1个)
回答1:

// 判断文件是否存在,不存在则创建,否则读取值显示到窗体
if (!File.Exists("F:\\TestTxt.txt"))
{

FileStream fs1 = new FileStream("F:\\TestTxt.txt", FileMode.Create, FileAccess.Write);//创建写入文件
StreamWriter sw = new StreamWriter(fs1);
sw.WriteLine(this.textBox3.Text.Trim() + "+" + this.textBox4.Text);//开始写入值

sw.Close();
fs1.Close();

}
else
{
FileStream fs = new FileStream("F:\\TestTxt.txt", FileMode.Open, FileAccess.Write);
StreamWriter sr = new StreamWriter(fs);
sr.WriteLine(this.textBox3.Text.Trim() + "+" + this.textBox4.Text);//开始写入值
sr.Close();
fs.Close();
}