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

发布于 2024-12-04 16:03:14 字数 207 浏览 2 评论 0 原文

我需要实现类似于记事本的保存选项的功能。假设我在 RichTextBox 旁边放置了一个按钮,我想要的是,当单击此按钮时,将打开一个对话框,该对话框看起来类似于 保存时出现的对话框当被点击时。我想通过在保存对话框框中输入文件名来以文本格式保存RichTextBox的内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

誰ツ都不明白 2024-12-11 16:03:14
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);
  }
}
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);
  }
}
塔塔猫 2024-12-11 16:03:14

对于WPF,您应该使用此SaveFileDialog< /a>.

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);
    }
}

For WPF you should use this 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);
    }
}
伏妖词 2024-12-11 16:03:14

这适用于文本文件,并在 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);
}

This works for text files and was tested in 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);
}
江挽川 2024-12-11 16:03:14
SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.ShowDialog();
OutputStream ostream = new FileOutputStream(new File(sfDialog.FileName));
WorkBook.write(ostream);
ostream.close();
SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.ShowDialog();
OutputStream ostream = new FileOutputStream(new File(sfDialog.FileName));
WorkBook.write(ostream);
ostream.close();
不知所踪 2024-12-11 16:03:14

误读了问题 - Ray 的答案对 OP 有效

这仅适用于 Windows 窗体。

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

并使用类似的内容保存文件(请参阅此处) :

rtf.SaveFile(dialog.FileName);

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

This works only in Windows Forms.

You should take a look at the SaveFileDialog class: 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);
通知家属抬走 2024-12-11 16:03:14

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文