Solved

displaying a web page on page load in a windows form C#

Posted on 2011-02-10
6
356 Views
Last Modified: 2012-05-11
I have a web page called home.htm in the root directory of the project file for my windows form. I am using a web browser control. I would like it to display the htm page on
Page_Load
{

}
I tried webrowser1.Navigate(new uri = "file://home.htm");
But I get an error - does anyone know how to do this
0
Comment
Question by:r3nder
6 Comments
 
LVL 7

Expert Comment

by:SterlingMcClung
ID: 34868488
You will have to use the full path to the file in the uri.  You should be able to use My.Application.Info.DirectoryPath to get the directory of your application.
webbrowser1.Navigate(new Uri(String.Format("http://{1}/file.htm",My.Application.Info.DirectoryPath))

Open in new window

0
 
LVL 12

Expert Comment

by:Mamtha1982
ID: 34868498
You can do something like:

string html = System.IO.File.ReadToEnd("your html file path");
webBrowser1.DocumentText = html

0
 
LVL 12

Expert Comment

by:Mamtha1982
ID: 34868522
Sorry:

string html = System.IO.File.ReadAllText("your html file path");
webBrowser1.DocumentText = html
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
chandra_darbha earned 500 total points
ID: 34868567
The code attached will work for what you are trying to do. But, in general, at the end of development your application (i.e the exe ) should have the Htm file exactly 2 levels above it.

 
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate(string.Format("file:///{0}/../../file.html", System.IO.Path.GetDirectoryName(Application.ExecutablePath)));
        }
    }

Open in new window


Ideal case is to place the htm in the same location as the exe or in any standard location which you are sure is the deployment pattern
0
 
LVL 8

Expert Comment

by:cubaman_24
ID: 34870148
Hello:
If you exe and html are in the same directory, use this code. It works.

Best regards.
Uri pageUri= new Uri(Path.GetFullPath("MyHtmlPage.htm"));

Open in new window

0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 34871368
perfect
Thank you........and the only one that worked
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now