我用C#做了个记事本,用richTextBox1.LoadFile()的方法打开windows的记事本显示是乱码,向各位高手请教该用什么打开方法可以实现?
如下是我写的代码,如何修改可以实现与windows的记事本兼容打开? private void 打开OToolStripButton_Click(object sender, EventArgs e)
{
OpenFileDialog fileone = new OpenFileDialog();
fileone.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|所有文件(*.*)|*.*";
fileone.FilterIndex = 1;
if (fileone.ShowDialog() == DialogResult.OK)
{
try
{
this.richTextBox1.LoadFile(fileone.FileName); }
catch (Exception)
{
MessageBox.Show("Oh,您的打开不成功!");
}
}
}
如下是我写的代码,如何修改可以实现与windows的记事本兼容打开? private void 打开OToolStripButton_Click(object sender, EventArgs e)
{
OpenFileDialog fileone = new OpenFileDialog();
fileone.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|所有文件(*.*)|*.*";
fileone.FilterIndex = 1;
if (fileone.ShowDialog() == DialogResult.OK)
{
try
{
this.richTextBox1.LoadFile(fileone.FileName); }
catch (Exception)
{
MessageBox.Show("Oh,您的打开不成功!");
}
}
}
openfiledialog.InitialDirectory = "c:\\";
openfiledialog.Filter = "文本文件|*.txt|RTF文件|*.rtf|所有文件|*.*";
openfiledialog.FilterIndex = 1;
openfiledialog.RestoreDirectory = false;
if (openfiledialog.ShowDialog() == DialogResult.OK)
{
try
{
string filename = openfiledialog.FileName;
childForm newform = new childForm();
newform.MdiParent = this;
string fileext = new System.IO.FileInfo(filename).Extension;
switch (fileext.ToLower())
{ case ".txt":
newform.richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText);
break;
case ".rtf":
newform.richTextBox1.LoadFile(filename, RichTextBoxStreamType.RichText);
break;
}
newform.Text = System.IO.Path.GetFileNameWithoutExtension(filename) + "--简易写字板";
newform.SaveFileName = filename;
newform.Show();
this.toolStripMain.Enabled = true;
this.toolStripFormat.Enabled = true;
newform.SetTools += new childForm.SetToolsEventHandler(SetTools);
}
catch
{
MessageBox.Show("文件格式不正确", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
那些文本可能会被人调过编码格式,判断一下是UTF-8或是GB2312的,读的时候加上编码应该就可以正常显示了.
{
OpenFileDialog fileone = new OpenFileDialog();
fileone.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|所有文件(*.*)|*.*";
fileone.FilterIndex = 1;
if (fileone.ShowDialog() == DialogResult.OK)
{
try
{
string filename = openfiledialog.FileName;
string fileext = new System.IO.FileInfo(filename).Extension;
switch (fileext.ToLower())
{ case ".txt":
newform.richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText);
break;
case ".rtf":
newform.richTextBox1.LoadFile(filename, RichTextBoxStreamType.RichText);
break;
}
}
catch (Exception)
{
MessageBox.Show("Oh,您的打开不成功!");
}
}
}这样试过了没有?
private void 打开OToolStripButton_Click(object sender, EventArgs e)
{
OpenFileDialog fileone = new OpenFileDialog();
fileone.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|所有文件(*.*)|*.*";
fileone.FilterIndex = 1;
if (fileone.ShowDialog() == DialogResult.OK)
{
try
{
OpenFileDialog file =new OpenFileDialog();
string filename = file.FileName;
string fileext = new System.IO.FileInfo(filename).Extension;
switch (fileext.ToLower())
{ case ".txt":
this.richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText);
break;
case ".rtf":
this.richTextBox1.LoadFile(filename, RichTextBoxStreamType.RichText);
break;
}
}
catch (Exception)
{
MessageBox.Show("Oh,您的打开不成功!");
}
}
改成this.richTextBox1.LoadFile(fileone.FileName,RichTextBoxStreamType.PlainText);
改成this.richTextBox1.LoadFile(fileone.FileName,RichTextBoxStreamType.PlainText);”
可以打开windows的记事本,但是打开c#记事本时就是乱码了,这里如何解决呢?