• 0

[C#]How can I save a web page as a html file?


Question

9 answers to this question

Recommended Posts

  • 0

I find a way

my WebBrowser object name is "wb"

    object doc = wb.Document;
    mshtml.HTMLDocumentClass c = (mshtml.HTMLDocumentClass) doc;

    System.IO.StreamWriter str = null;
    try
    {
    	str = new StreamWriter("C:\\saved.html");
    	str.Write( c.documentElement.outerHTML );
    }
    catch 
    {
    }
    finally
    {
    	if( str != null )
    	{
      str.Close();
    	}
    }

it just the way, in a really application you will need some additional codes

such as check if there is no pages loaded, and to open a save dialog and so on...

  • 0
  THE_BAT said:
I find a way

my WebBrowser object name is "wb"

 ? ?object doc = wb.Document;
 ? ?mshtml.HTMLDocumentClass c = (mshtml.HTMLDocumentClass) doc;

 ? ?System.IO.StreamWriter str = null;
 ? ?try
 ? ?{
 ? ?	str = new StreamWriter("C:\\saved.html");
 ? ?	str.Write( c.documentElement.outerHTML );
 ? ?}
 ? ?catch 
 ? ?{
 ? ?}
 ? ?finally
 ? ?{
 ? ?	if( str != null )
 ? ?	{
 ? ? ?str.Close();
 ? ?	}
 ? ?}

it just the way, in a really application you will need some additional codes

such as check if there is no pages loaded, and to open a save dialog and so on...

586480544[/snapback]

Thank you , and then how to read the html file and show it with object WebBrowser?? Ine:pcon' alt=':D' />guy in this filed. :p :D :cool: :D

  • 0
  THE_BAT said:
I find a way

my WebBrowser object name is "wb"

 ? ?object doc = wb.Document;
 ? ?mshtml.HTMLDocumentClass c = (mshtml.HTMLDocumentClass) doc;

 ? ?System.IO.StreamWriter str = null;
 ? ?try
 ? ?{
 ? ?	str = new StreamWriter("C:\\saved.html");
 ? ?	str.Write( c.documentElement.outerHTML );
 ? ?}
 ? ?catch 
 ? ?{
 ? ?}
 ? ?finally
 ? ?{
 ? ?	if( str != null )
 ? ?	{
 ? ? ?str.Close();
 ? ?	}
 ? ?}

it just the way, in a really application you will need some additional codes

such as check if there is no pages loaded, and to open a save dialog and so on...

586480544[/snapback]

These codes really works well on English WebPage, but not on Chinese WebPage, :yes:u know how to set the coding??? :yes:

  • 0
  zhwcn said:
These codes really works well on English WebPage, but not on Chinese WebPage,  do you know how to set the coding??? :yes:

586481435[/snapback]

i didn't work with Chinese before

but try to change the Encoding

change the following line

str = new StreamWriter("C:\\saved.html", false, System.Text.Encoding.Unicode);

or try the other Encoding

System.Text.Encoding.ASCII
System.Text.Encoding.BigEndianUnicode
System.Text.Encoding.UTF7  
System.Text.Encoding.UTF8

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.