Invoke 'Save As' Dialog to save WebBrowser control contents

A

Artie

Hi,

I have a WebBrowser control displaying some formatted HTML.

I need to be able to save the formatted HTML to a file, using the
common Windows 'Save As' dialog, and preferably be able to pre-
populate the File Name for saving.

What's the easiest way to achieve this?

Cheers

Artie
 
N

Nicholas Paldino [.NET/C# MVP]

Artie,

Why not use the SaveFileDialog? Since you are using the WebBrowser
control, you are using Windows Forms, and you can easily use the
SaveFileDialog to do what you want.
 
S

Shug

Artie,

    Why not use the SaveFileDialog?  Since you are using the WebBrowser
control, you are using Windows Forms, and you can easily use the
SaveFileDialog to do what you want.

--
          - Nicholas Paldino [.NET/C# MVP]
          - (e-mail address removed)




I have a WebBrowser control displaying some formatted HTML.
I need to be able to save the formatted HTML to a file, using the
common Windows 'Save As' dialog, and preferably be able to pre-
populate the File Name for saving.
What's the easiest way to achieve this?

Artie- Hide quoted text -

- Show quoted text -

Hi Nicholas,

I looked briefly at SaveFileDialog and most of it looked ok, but I
couldn't see where I can populate the actual content to be saved,
which would be my formatted HTML.

Have I missed some really obvious way of doing this?

Artie
 
P

Peter Duniho

I looked briefly at SaveFileDialog and most of it looked ok, but I
couldn't see where I can populate the actual content to be saved,
which would be my formatted HTML.

Have I missed some really obvious way of doing this?

Nicholas obviously thought your original question was simply about how to
get a dialog that the user can use to specify a file to save. It's what I
thought too.

If you're asking also for help in actually saving the data, then the
dialog isn't going to do that for you. You still will want to use the
dialog to get the path for where to save the data. But then you need to
use that path to create some kind of file i/o class instance that can
write the data for you (I think the StreamWriter class is probably a good
choice for what you're doing...if you've got the HTML as a string, you can
just write that whole string to the file with a StreamWriter instance).

Pete
 
A

Artie

Nicholas obviously thought your original question was simply about how to  
get a dialog that the user can use to specify a file to save.  It's whatI  
thought too.

If you're asking also for help in actually saving the data, then the  
dialog isn't going to do that for you.  You still will want to use the  
dialog to get the path for where to save the data.  But then you need to 
use that path to create some kind of file i/o class instance that can  
write the data for you (I think the StreamWriter class is probably a good  
choice for what you're doing...if you've got the HTML as a string, you can 
just write that whole string to the file with a StreamWriter instance).

Pete

Hi Pete,

Yes, sorry for not being clear enough in my original post. The Dialog
itself was only part of the issue.

As you suggested, the dialog worked fine to specify the file itself
and the StreamWriter worked perfectly to save the contents to the
previously specified file.

Having set up the saveFileDialog, the following worked to save the
contents:

if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if ((stream = saveFileDialog.OpenFile()) != null)
{
StreamWriter writer = new StreamWriter(stream);
writer.Write(message.HTML);
writer.Close();
}

...
 
P

Peter Duniho

[...]
Having set up the saveFileDialog, the following worked to save the
contents:

if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if ((stream = saveFileDialog.OpenFile()) != null)
{
StreamWriter writer = new StreamWriter(stream);
writer.Write(message.HTML);
writer.Close();
}

Try instead:

if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if ((stream = saveFileDialog.OpenFile()) != null)
{
using (stream)
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(message.HTML);
writer.Close();
}
}
}

You need to dispose anything that implements IDisposable.

Also, I think it's a little odd that SaveFileDialog.OpenFile() doesn't
throw an exception but instead returns null on failure. The docs aren't
clear on this...they don't document any exceptions, and the sample code
shows checking for null, but the return value also isn't documented as
returning null. You'll probably want to test at least one failure case to
make sure the handling is correct (actually, you should anyway, but hereI
think it's especially important).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top