我需要实现类似于 Notepads 的保存选项。假设我在一个 RichTextBox
旁边放置了一个按钮,当点击此按钮时,将会弹出一个对话框,该对话框看起来类似于单击 另存为 时出现的对话框。我希望能够以文本格式保存 RichTextBox 的内容,并在 保存对话框 中输入文件名。
我需要实现类似于 Notepads 的保存选项。假设我在一个 RichTextBox
旁边放置了一个按钮,当点击此按钮时,将会弹出一个对话框,该对话框看起来类似于单击 另存为 时出现的对话框。我希望能够以文本格式保存 RichTextBox 的内容,并在 保存对话框 中输入文件名。
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);
}
}
_SD.FileName = "Untitled";
。 - Overlock对于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);
}
}
这适用于文本文件,并在 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);
}
SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.ShowDialog();
OutputStream ostream = new FileOutputStream(new File(sfDialog.FileName));
WorkBook.write(ostream);
ostream.close();
误读问题 - Ray 的答案适用于 OP
这仅适用于 Windows Forms。
您应该查看 SaveFileDialog 类:http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx
然后使用类似以下方式保存文件(请参见此处):
rtf.SaveFile(dialog.FileName);
System.Windows.Forms
命名空间替换为 Microsoft.Win32.SaveFileDialog
,我认为你就可以开始了。 - Joel A. Villarreal Bertoldi