我正在尝试创建一个记笔记应用程序(第一个Windows窗体应用程序)。到目前为止,我已经设法将.txt文件读入RichTextBox。我正在尝试使程序创建并保存包含读入时读取的.txt文件内容的.txt文件。因此,当用户从文件中添加文本时,它会在文件夹中创建一个新文件。应用程序的根目录。请参阅下面的代码。任何建议将不胜感激。干杯
private void button1_Click(object sender, EventArgs e)
{
//read in a .txt file
OpenFileDialog op = new OpenFileDialog();
if (op.ShowDialog() == DialogResult.OK)
richTextBox1.LoadFile(op.FileName, RichTextBoxStreamType.PlainText);
this.Text = op.FileName;
string fileName = op.FileName;
//create new .txt file contaning module notes
System.IO.StreamWriter file = new System.IO.StreamWriter("\\"+fileName);
file.WriteLine(fileName);
file.Close();
}
答案 0 :(得分:1)
经过测试和工作
//read in a .txt file
OpenFileDialog op = new OpenFileDialog();
if (op.ShowDialog() == DialogResult.OK)
richTextBox1.LoadFile(op.FileName, RichTextBoxStreamType.PlainText);
this.Text = op.FileName;
string fileName = Path.Combine(Application.CommonAppDataPath, Path.GetFileName(op.FileName));
File.WriteAllText(fileName, "test");
答案 1 :(得分:0)
using(var myfile = File. CreateText(path) ) {
myfile.WriteLine("hi");
}