• 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.
  • Posts

    • Running Qbittorrent 5.1.0 on Win11 just fine.
    • I love this game since its first version and it seems that it's only getting better!
    • Oh my goooooooooooooooooooooooooood!!!!!!!!!!!!!!!!!!!!!!!!!!! Yes!!! Super Resident Evil fan here, in case you didn't notice 😅. And Raccoon City is my favorite setting in the entire RE universe. This is a dream come true.
    • Just found this Taskbar Buddy program (still in beta) - 'It lets you turn literally any GIF into your own personal desktop pet.  Thought I would throw some more eyes onto it, (I'm not involved just sharing) https://orange-boy-0.itch.io/taskbar-buddy  
    • Resident Evil Requiem announced for early 2026, taking the story back to Raccoon City by Pulasthi Ariyasinghe The Summer Game Fest showcase today ended with a bang as Capcom delivered the announcement trailer for its next mainline Resident Evil game, finally showing off what it has in store for the ninth entry. Aptly dubbed Resident Evil Requiem, the game is described as a "heart-stopping experience that will chill you to your core." Watch the newly revealed announcement trailer above. The single-player horror game will follow Grace Ashcroft as a protagonist, an FBI agent investigating a series of unusual murders at a hotel named the Remnant Hotel, all linked to her mother's mysterious death. While a new face to the series, she may be related to a character from another Resident Evil game, Outbreak, from back in the day, though Capcom is yet to confirm any ties just yet. Rumors have suggested that Leon is returning in this entry as a playable character too, but the debut trailer did not drop any hints about this project being a dual-protagonist adventure. "A new era of survival horror begins in 2026," says Capcom regarding its latest Resident Evil experience. "Technological advancements combined with the development team's depth of experience combine in a story with rich characters and gameplay that's more immersive than ever before." It looks like the title is returning to its survival horror roots after going for more action-orientated entries in recent times. The original Raccoon City seems to be the setting again too, letting players return to ground zero of the virus outbreak, just in time for the series to hit its 30th anniversary. Resident Evil Requiem is launching across PC, Xbox Series X|S, and PlayStation 5 on February 27, 2026.
  • Recent Achievements

    • Week One Done
      daelos earned a badge
      Week One Done
    • One Month Later
      daelos earned a badge
      One Month Later
    • Mentor
      Karlston went up a rank
      Mentor
    • One Month Later
      EdwardFranciscoVilla earned a badge
      One Month Later
    • One Month Later
      MoyaM earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      492
    2. 2
      snowy owl
      254
    3. 3
      +FloatingFatMan
      252
    4. 4
      ATLien_0
      215
    5. 5
      Xenon
      152
  • Tell a friend

    Love Neowin? Tell a friend!