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.
How to add Save file Dialog Box using C#
Asked Answered
Hmm, how did you know to use the [savefiledialog] tag? Fake question? –
Labourer
@Hans: I was typing out savefile and then I was suggested this tag. Prior to the answers below, I though this tag literally meant "Dialogue to Save file". That is, I didn't know it was name of a Control. –
Whisk
Well, credit to SO to give you the answer before you post it. –
Labourer
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);
}
}
Thanks for the detailed post which clarifies more on file types and Title. What about adding a default file name, how to do that? –
Whisk
Only add over the if block this code
_SD.FileName = "Untitled";
–
Bulge 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);
}
}
what about saving the content. The XAML tag will possibly open up a Dialog Box, but how is the content supposed to get save? –
Whisk
@Sam, OpenFile requires less permissions. Also isn't FileOutputStream java?? –
Gaudery
i use FileOutputStream in C# 2005.Used like this.. java.io.OutputStream ostream = new java.io.FileOutputStream(new java.io.File(FileName)); WorkBook.write(ostream); ostream.close(); –
Overissue
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);
}
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);
Thanks for the link. What about the content of the file which is to come from RichTextBox. How is that content going to be saved to the corresponding file? –
Whisk
Added some more info on saving the file from the RTF. –
Og
Replace the
System.Windows.Forms
namespace with Microsoft.Win32.SaveFileDialog
and I think you're good to go. –
Og RichTextBox doesn't have a SaveFile in WPF –
Gaudery
Damn, I missed that one. You're right. Your answer solves the OP's question perfectly, my bad. –
Og
SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.ShowDialog();
OutputStream ostream = new FileOutputStream(new File(sfDialog.FileName));
WorkBook.write(ostream);
ostream.close();
Does FileOutputStream exist in .net? –
Gaudery
There is a SaveFileDialog
component which you can use, read here to find out how it works and a working sample.
© 2022 - 2024 — McMap. All rights reserved.