我是一名大一新生,想用C#做一个记事本,测试时发现记事本可以保存文件,但格式不对,主要是无法保留制表符,也就是不会保存换行,保存部分的代码如下:
string postion;
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
FolderBrowserDialog save = new FolderBrowserDialog();
DialogResult result = save.ShowDialog();
if( result == DialogResult.OK )
postion = save.SelectedPath;
***************************这个是用Stream类写的********************************************
string path = string.Format(@"{0}\{1}.txt", postion, "未定");
Stream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(stream);
sw.Write(richTextBox1.Text);
sw.Close();
stream.Close();
***************************这个是用File类写的***********************************************
//File.AppendAllText(path, richTextBox1.Text);
}
希望大侠帮帮忙,告诉我原因或者给出一个解决方案,我不胜感激。
string postion;
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
FolderBrowserDialog save = new FolderBrowserDialog();
DialogResult result = save.ShowDialog();
if( result == DialogResult.OK )
postion = save.SelectedPath;
***************************这个是用Stream类写的********************************************
string path = string.Format(@"{0}\{1}.txt", postion, "未定");
Stream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(stream);
sw.Write(richTextBox1.Text);
sw.Close();
stream.Close();
***************************这个是用File类写的***********************************************
//File.AppendAllText(path, richTextBox1.Text);
}
希望大侠帮帮忙,告诉我原因或者给出一个解决方案,我不胜感激。
sw.Write(richTextBox1.Text.Replace("\n", "\r\n"));
sw.WriteLine(s);
每行的后面要加 "\n\r" 才是回车了.