How to add Save file Dialog Box using C#
Asked Answered
W

6

5

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.

Whisk answered 14/9, 2011 at 11:55 Comment(3)
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
B
10
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);
  }
}
Bulge answered 14/9, 2011 at 13:1 Comment(2)
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
G
7

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);
    }
}
Gaudery answered 14/9, 2011 at 11:59 Comment(3)
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
D
2

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);
}
Dochandorrach answered 17/2, 2014 at 22:57 Comment(0)
O
1

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);
Og answered 14/9, 2011 at 11:58 Comment(5)
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 WPFGaudery
Damn, I missed that one. You're right. Your answer solves the OP's question perfectly, my bad.Og
O
1
SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.ShowDialog();
OutputStream ostream = new FileOutputStream(new File(sfDialog.FileName));
WorkBook.write(ostream);
ostream.close();
Overissue answered 14/9, 2011 at 12:0 Comment(1)
Does FileOutputStream exist in .net?Gaudery
L
0

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

Leotaleotard answered 14/9, 2011 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.