0

在创建文本文件之后,它在我添加的richtextbox 中转义了“新行”,告诉我如何解决该问题 + 在创建具有大名称和大量数据的文件时,它会给出异常。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace Saver
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
    string path = "";

    public MainForm()
    {
        //
        // The InitializeComponent() call is required for Windows Forms designer            
        //
        InitializeComponent();

        //
        // TODO: Add constructor code after the InitializeComponent() call.
        //
    }

    void Button1Click(object sender, EventArgs e)
    {
        if((textBox1.Text != "")&&(richTextBox1.Text != ""))
        {
            if(radioButton1.Checked == true)
                path = @"C:\Users\M.Waqas\Desktop\Saver\Saver\Files\";

            path += textBox1.Text + ".txt";

            FileStream fs = new FileStream(path,FileMode.Create);
            StreamWriter wr = new StreamWriter(fs);
            wr.Write(richTextBox1.Text);
            MessageBox.Show("Your file create on :" + "\n" + path);
            textBox1.Clear();
            richTextBox1.Clear();
            wr.Close();
            fs.Close();


        }
    }
}

}

4

1 回答 1

2

使用 .NET 方法而不是重写它们怎么样?

void Button1Click(object sender, EventArgs e)
{
      try{
           if(!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(richTextBox1.Text))
           {
                if(radioButton1.Checked) 
                     path = @"C:\Users\M.Waqas\Desktop\Saver\Saver\Files\"; 

                path = System.IO.Path.Combine(path, textBox1.Text  ".txt");

                richTextBox1.SaveFile(path);  

           } else
                MessageBox.Show("No data");

      } catch (Exception x){
           MessageBox.Show("Error: "+x);
      }
}

注意:路径是从 OP 复制过来的,应该使用 SaveFileDialog 而不是硬编码路径(更糟糕的是 - 与用户相关的硬编码路径)。对于未选中单选按钮的情况,路径代码可能不完整。

于 2013-02-11T17:49:11.967 回答