-
March 4th, 2010, 04:13 PM
#1
How to create a save file dialog in C#
Hi there
I have code written that will create a document and insert some content into it but as soon as it has done this, instead of using the Save() or SaveAs() methods that overwrite anything in a predefined location, I want a save file dialog box to appear asking for where the file needs to be saved. How do I do this?
thanks in advance.
-
March 4th, 2010, 05:06 PM
#2
Re: How to create a save file dialog in C#
This sample code does this:
Code:
string path="";
FileDialog dlg=new SaveFileDialog();
DialogResult result = dlg.ShowDialog();
if(result == DialogResult.OK)
{
path=dlg.FileName;
}
-
March 4th, 2010, 05:17 PM
#3
Re: How to create a save file dialog in C#
Also make sure to call Dispose() on the dialog, i.e., wrap it in a using block.
-
March 4th, 2010, 05:50 PM
#4
Re: How to create a save file dialog in C#
To clarify:
Code:
using (FileDialog dlg = new SaveFileDialog())
{
if (dlg.ShowDialog().Equals(DialogResult.OK)
{
String path = dlg.FileName;
File.WriteAllText(path, /* your data string */); // or WriteAllLines/Bytes etc
}
}
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
March 5th, 2010, 03:19 AM
#5
Re: How to create a save file dialog in C#
Originally Posted by dovobet
Hi there
I have code written that will create a document and insert some content into it but as soon as it has done this, instead of using the Save() or SaveAs() methods that overwrite anything in a predefined location, I want a save file dialog box to appear asking for where the file needs to be saved. How do I do this?
thanks in advance.
There are samples in MSDN:
http://msdn.microsoft.com/en-us/libr...iledialog.aspx
http://msdn.microsoft.com/en-us/library/sfezx97z.aspx
-
March 5th, 2010, 08:16 PM
#6
Re: How to create a save file dialog in C#
thank you again, the code works perfectly
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|