日期:2014-05-18  浏览次数:20816 次

C#求记事本保存文件的代码格式
在C#中如何实现打开/保存文件格式(.txt&所有文件)?
  具体代码怎样?
  我的保存文件代码如下(但是对话框里面没有选择保存的文件格式):

  private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
  {
  if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  {
  saveFileDialog1.FileName = "*.txt";
  saveFileDialog1.ShowDialog();
  StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
  sw.Write(richTextBox1.Text);
  sw.Close();

  }  

打开文件代码(对话框也是没有打开文件的类型如.txt):
  private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
  {
  OpenFileDialog dlg = new OpenFileDialog();
  dlg.Title = "选择数据库文件...";
  dlg.Filter = "Microsoft Access 2000 文件(*.mdb)|*.mdb|所有文件(*.*)|*.*";
  if (dlg.ShowDialog() == DialogResult.OK)
  {
  richTextBox1.Text = dlg.FileName;
  }
  }

 

------解决方案--------------------
 

打开文件代码(对话框也是没有打开文件的类型如.txt): 
private void openFileDialog1_FileOk(object sender, CancelEventArgs e) 

OpenFileDialog dlg = new OpenFileDialog(); 
dlg.Title = "选择数据库文件..."; 
dlg.Filter = "txt files (*.txt)|*.txt"; //其实你的那个也有,就是要下拉选择框.
if (dlg.ShowDialog() == DialogResult.OK)

richTextBox1.Text = dlg.FileName; 

}
------解决方案--------------------
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) 

if (saveFileDialog1.ShowDialog() == DialogResult.OK) //你已经打开对话框并选择文件后OK了,才saveFileDialog1.FileName = "*.txt"; 是不是晚了

saveFileDialog1.FileName = "*.txt"; 
saveFileDialog1.ShowDialog(); 
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); 
sw.Write(richTextBox1.Text); 
sw.Close(); 

}
------解决方案--------------------
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件|*.*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK) 

StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); 
sw.Write(richTextBox1.Text); 
sw.Close(); 

}