Solved

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

Posted on 2011-02-10
6
357 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

813 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

17 Experts available now in Live!

Get 1:1 Help Now