0

I am currently developing an application for my website (file downloader/updater) and I would like to create a block within my application that reads and displays a html web page from my website within the C# application.

This html/css page would be very simple (recent change log or dev blog).

How can I red and display the html page within my C# application?

For example, I want the html page content to be displayed in the blank space in this application http://puu.sh/5BFcv.png

2 Answers 2

2

If you're developing for Winforms/WPF, you should be able to use the standard WebBrowser control to display HTML. Although it's powered on IE7+ I believe, you stated that you only need to display a bit of data, so it'll probably work out fine.

Or you can take a look at the HTML Renderer project on Codeplex and use that if the default WebBrowser control doesn't work out for you (broken pages).

Also, a side note: For something as simple as a changelog delivering mechanism, I'd personally avoid using a Webbrowser to display HTML. Why not retrieve the changelog in plain text format and populate the information natively.

Sample code you can use to download a text file from a web server and set it as a label's text:

    public void Main()
    {
        using (WebRequest req = new WebRequest())
        {
            string downloadedChangelog = req.DownloadString(new Uri("http://www.site.com/changelog.txt"));
            changelogLabel.Text = downloadedChangelog;      
        }
    }

Or, if you want to make the downloading string part asynchronous (won't freeze the UI), you can do this:

    public void Main()
    {
        using (WebClient client = new WebClient())
        {
            client.DownloadStringCompleted += client_DownloadStringCompleted;
            client.DownloadStringAsync(new Uri("http://www.google.com"));
        }
    }

    private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        changelogLabel.Text = e.Result.ToString();
    }

Make sure to put a using System.Net; statement at the top of your code, or type it all out.

Sign up to request clarification or add additional context in comments.

8 Comments

How would you go about retrieving it in plain text and displaying it within the application? (I am pretty noob to C# development and have only taken some basic courses in college for C#, nothing as extensive as this).
You can use the HttpWebRequest or WebClient class to fetch a page's source and store it in a string. Then, you can change a Label's text to the downloaded string!
Say I have a changelog.txt file located on my web server, how could I go about reading that file and displaying it line by line within my application?
Formatting is messed up, I'll edit my original answer for the sample code
nevermind, figured out the error. I needed to make a call to form1 since the method is staic and the field I am calling is not. via Form1 form1 = new Form1();
|
0

Try to add a WebBrowser controls to your app.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.