在创建了文本文件之后,它正在转移我要添加的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();
}
}
}
}
答案 0 :(得分: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而不是硬编码路径(更糟糕的是 - 用户相关的硬编码路径)。对于未选中单选按钮的情况,路径代码可能不完整。