CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Displaying website source code in a richtextbox

  1. #1
    Join Date
    Jan 2010

    Question Displaying website source code in a richtextbox

    How would I fetch and then display source code from a website into a richtextbox in c#?
    i.e. if I were to input in "google.com" then on one side I'll have the browser that travelled to google, but on the other side is a richtextbox with the html source code from google.

  2. #2
    Join Date
    Jun 2001
    Melbourne/Aus (C# .Net 4.0)

    Re: Displaying website source code in a richtextbox

    I'm interested as to why it has be be a Rich Text Box and not an ordinary Text Box. Are you expecting the syntax of the HTML source code to be automatically highlighted in the Rich Text Box? As that will not happen and it will take you a looooooooooooooong time to write the code to get it to happen perfectly.

    However, to get the source code of a URL, you may wish to try:
    public static String ReadAllText(String url)
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        using (WebResponse webResponse = webRequest.GetResponse())
            using (StreamReader responseReader = new StreamReader(webResponse.GetResponseStream()))
                return responseReader.ReadToEnd();
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Nov 2002
    .NET 3.5 VS2008

    Re: Displaying website source code in a richtextbox


    You can also use the WebClient class from the System.Net namespace. Here's a link to the details in MSDN. I think I like rliq's suggestion (i.e. WebRequest approach) though...I was looking for that class but missed it somehow. Good spot rliq.
    Last edited by nelo; January 20th, 2010 at 07:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)