小编joh*_*ohn的帖子

在 C# 上将 richTextBox 内容保存到 .txt 文件

当你编写这段代码时:

    string path = @"c:\temp\MyTest.txt";
    // This text is added only once to the file.
    if (!File.Exists(path))
    {
        // Create a file to write to.
        string[] createText = { "Hello", "And", "Welcome" };
        File.WriteAllLines(path, createText);
    }
Run Code Online (Sandbox Code Playgroud)

结果

现在我想将 richTextBox 内容的每一行保存到 .txt 文件的新行,如结果图像。我编写代码如下:

private void button1_Click(object sender, EventArgs e)
{
     richTextBox1.AppendText(textBox1.Text +"\n");
     System.IO.File.WriteAllText(@"C:\Users\Mohammad_Taghi\Desktop\ab\a.txt", richTextBox1.Text + Environment.NewLine);            
}
Run Code Online (Sandbox Code Playgroud)

但是,结果是 结果2

c# richtextbox winforms

5
推荐指数
1
解决办法
6773
查看次数

标签 统计

c# ×1

richtextbox ×1

winforms ×1