我正在从文本框中写入一些文本到。txt文件,在40个字符之后,它只是放了一个'…'
代码如下:
//defining the path as a string
string fileName1 = @"C:Users-----AppDataLocal" + nameTextBox.Text + " FileName.txt";
//checking if the file exists
if (!File.Exists(fileName))
{
//if file does exist, open the file and edit it
using (StreamWriter writer = File.CreateText(fileName))
{
//using stream writer to write the file System.IO.StreamWriter(@"C:Users-----AppDataLocal" + NameTextbox.Text + "FileName.txt"); //open the file for writing.
writer.Write(writingTextBox.Text); //write text box contents to the file.
writer.Close();
writer.Dispose();
}
}
else
{
//if the file does not exist, create new file
System.IO.StreamWriter writer = new System.IO.StreamWriter(@"C:Users-----AppDataLocal" + nameTextBox.Text + "FileName.txt");
writer1.Write(writingTextBox); //write the textbox contents to the file.
writer1.Close();
writer1.Dispose();
}
请帮忙,
谢谢。
如果你只是在写简单的文本数据,你只是想每次覆盖它,那么你最好使用WriteAllText
string fileName = @"C:Users-----AppDataLocal" + nameTextBox.Text + " FileName.txt";
File.WriteAllText(fileName, writingTextBox.Text));
不需要检查文件是否存在并进行不同的处理。
如果你想追加文件,那么你也可以使用AppendAllText
。但是你必须首先检查文件是否存在。