Solved

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

Posted on 2011-02-10
6
358 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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…

840 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