locked
FAQ: How do I save a web page in VB.NET? RRS feed

Answers

  • Here are several approaches:


    1) You can directly save a web page as an .html file using the System.Net.WebClient class.


    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) Handles Button1.Click
            Dim myWebClient As New System.Net.WebClient
            myWebClient.DownloadFile("http://forums.microsoft.com/MSDN/default.aspx", _
                            "C:\msdn.html")
        End Sub
    End Class
    
    

     

    2) You can save complete web pages as a .mht file using CDO and ADODB components.

    First Add References to the “Microsoft CDO For Windows 2000 Library” and “Microsoft ActiveX Data Objects 2.5 Library”.

    Project menu -> Add Reference... -> COM tab


    Imports ADODB
    Imports CDO
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) Handles Button1.Click
            SavePage("http://forums.microsoft.com/MSDN/default.aspx", "C:\msdn.mht")
        End Sub
    
        Private Sub SavePage(ByVal Url As String, ByVal FilePath As String)
            Dim iMessage As CDO.Message = New CDO.Message
            iMessage.CreateMHTMLBody(Url, _
            CDO.CdoMHTMLFlags.cdoSuppressNone, "", "")
            Dim adodbstream As ADODB.Stream = New ADODB.Stream
            adodbstream.Type = ADODB.StreamTypeEnum.adTypeText
            adodbstream.Charset = "US-ASCII"
            adodbstream.Open()
            iMessage.DataSource.SaveToObject(adodbstream, "_Stream")
            adodbstream.SaveToFile(FilePath, _
                      ADODB.SaveOptionsEnum.adSaveCreateOverWrite)
        End Sub
    
    End Class
    
    

     

    Related thread:

    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/c09c132c-5046-42f1-b1f8-11a5d16f458c/

     

    3) You can save web page content via the WebBrowser control.


    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) Handles MyBase.Load
            WebBrowser1.Navigate("http://forums.microsoft.com/MSDN/default.aspx")
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, _
                            ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
                            Handles WebBrowser1.DocumentCompleted
            ' Get entire html code and save as .html file
            Dim MSDNpage As String = WebBrowser1.DocumentText
            My.Computer.FileSystem.WriteAllText("C:\msdn1.html", MSDNpage, True)
            ' Get Body text and save as .txt file
            Dim MSDNpage As String = WebBrowser1.Document.Body.InnerText
            My.Computer.FileSystem.WriteAllText("C:\msdn2.txt", MSDNpage, True)
        End Sub
    
    End Class
    
    

     

    For more FAQ about Visual Basic .NET General, please see Visual Basic .NET General FAQ


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Friday, April 10, 2009 1:51 PM