我需要实现类似于记事本的保存选项。假设我在RichTextBox旁边放置了一个按钮,我想要的是,当单击该按钮时,将打开一个“对话”框,该对话框将与单击Save 时出现的按钮类似。我想通过在RichTextBox保存对话框中输入文件名,以文本格式保存的内容。
发布于 2011-09-14 13:01:13
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);
}
}发布于 2011-09-14 11:59:36
对于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);
}
}发布于 2014-02-17 22:57:53
这适用于文本文件,并在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);
}https://stackoverflow.com/questions/7415906
复制相似问题