如何使用C#添加保存文件对话框

时间:2022-01-05 09:58:36

I need to implement something similar to Notepads' save option. Assuming I have a button placed next to a RichTextBox, what I want is, when this button is clicked, a Dialogue box will open up, which will look similar to the one that appears when Save As is clicked. I would like to save the content of the RichTextBox in text format, by entering the name of file in the Save Dialogue box.

我需要实现类似于Notepads的保存选项。假设我在RichTextBox旁边放了一个按钮,我想要的是,当点击这个按钮时,会打开一个对话框,它看起来类似于点击另存为时出现的对话框。我想通过在“保存对话框”中输入文件名来以文本格式保存RichTextBox的内容。

6 个解决方案

#1


9  

private void Save_As_Click(object sender, EventArgs e)
{
  SaveFileDialog _SD = new SaveFileDialog(); 
  _SD.Filter = "Text File (*.txt)|*.txt|Show All Files (*.*)|*.*";
  _SD.FileName = "Untitled"; 
  _SD.Title = "Save As";
  if (__SD.ShowDialog() == DialogResult.OK)
  {
   RTBox1.SaveFile(__SD.FileName, RichTextBoxStreamType.UnicodePlainText);
  }
}

#2


7  

For WPF you should use this SaveFileDialog.

对于WPF,您应该使用此SaveFileDialog。

var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.Filter = "Rich Text File (*.rtf)|*.rtf|All Files (*.*)|*.*";
dialog.FileName = "Filename.rtf"; //set initial filename
if (dialog.ShowDialog() == true)
{
    using (var stream = dialog.OpenFile())
    {
       var range = new TextRange(myRichTextBox.Document.ContentStart,
                                 myRichTextBox.Document.ContentEnd);
       range.Save(stream, DataFormats.Rtf);
    }
}

#3


2  

This works for text files and was tested in WPF.

这适用于文本文件,并在WPF中进行了测试。

var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.Filter = "Text documents (.txt)|*.txt|All Files (*.*)|*.*"; 
dialog.FileName = "Filename.txt"; 
if (dialog.ShowDialog() == true)
{                
    File.WriteAllText(dialog.FileName, MyTextBox.Text);
}

#4


1  

SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.ShowDialog();
OutputStream ostream = new FileOutputStream(new File(sfDialog.FileName));
WorkBook.write(ostream);
ostream.close();

#5


1  

misread the question - Ray's answer is valid for OP

误读了这个问题 - 雷的回答对OP有效

This works only in Windows Forms.

这仅适用于Windows窗体。

You should take a look at the SaveFileDialog class: http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx

您应该查看SaveFileDialog类:http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx

And save the file using something like this (see here):

并使用类似的东西保存文件(见这里):

rtf.SaveFile(dialog.FileName);

#6


0  

There is a SaveFileDialog component which you can use, read here to find out how it works and a working sample.

您可以使用SaveFileDialog组件,在此处阅读以了解其工作原理和工作示例。

#1


9  

private void Save_As_Click(object sender, EventArgs e)
{
  SaveFileDialog _SD = new SaveFileDialog(); 
  _SD.Filter = "Text File (*.txt)|*.txt|Show All Files (*.*)|*.*";
  _SD.FileName = "Untitled"; 
  _SD.Title = "Save As";
  if (__SD.ShowDialog() == DialogResult.OK)
  {
   RTBox1.SaveFile(__SD.FileName, RichTextBoxStreamType.UnicodePlainText);
  }
}

#2


7  

For WPF you should use this SaveFileDialog.

对于WPF,您应该使用此SaveFileDialog。

var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.Filter = "Rich Text File (*.rtf)|*.rtf|All Files (*.*)|*.*";
dialog.FileName = "Filename.rtf"; //set initial filename
if (dialog.ShowDialog() == true)
{
    using (var stream = dialog.OpenFile())
    {
       var range = new TextRange(myRichTextBox.Document.ContentStart,
                                 myRichTextBox.Document.ContentEnd);
       range.Save(stream, DataFormats.Rtf);
    }
}

#3


2  

This works for text files and was tested in WPF.

这适用于文本文件,并在WPF中进行了测试。

var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.Filter = "Text documents (.txt)|*.txt|All Files (*.*)|*.*"; 
dialog.FileName = "Filename.txt"; 
if (dialog.ShowDialog() == true)
{                
    File.WriteAllText(dialog.FileName, MyTextBox.Text);
}

#4


1  

SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.ShowDialog();
OutputStream ostream = new FileOutputStream(new File(sfDialog.FileName));
WorkBook.write(ostream);
ostream.close();

#5


1  

misread the question - Ray's answer is valid for OP

误读了这个问题 - 雷的回答对OP有效

This works only in Windows Forms.

这仅适用于Windows窗体。

You should take a look at the SaveFileDialog class: http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx

您应该查看SaveFileDialog类:http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx

And save the file using something like this (see here):

并使用类似的东西保存文件(见这里):

rtf.SaveFile(dialog.FileName);

#6


0  

There is a SaveFileDialog component which you can use, read here to find out how it works and a working sample.

您可以使用SaveFileDialog组件,在此处阅读以了解其工作原理和工作示例。