0

I am using WebBrowser control in my C# winform application, And in URL property I am giving Path of local html file. Below is the code of of my PrintAds.Designer.cs

this.webBrowser1.Url = new System.Uri("D:\\PrintAds.htm", System.UriKind.Absolute);

Every thing is working fine, But I want to add this html file with the project, and I want to dynamically pass the URL to WebBrowser control, How do I do that?

This project is going to be used offline so I cant pass website URL.

4
  • Does it matter if the html file is only in your project folder? Commented Apr 11, 2014 at 7:37
  • @Zekth I didn't get it! Commented Apr 11, 2014 at 7:38
  • In your example your path is on the D: drive, but you can't access via absolute path in web browser. The way to access HTML file is to pass relative URL ( relative to your project ). The solution may be to copy the html file in a temp folder in your project and access to the copied file via relativ url. Commented Apr 11, 2014 at 7:41
  • @Zekth By giving the above path, I can access that html page in my WebBrowser control, But the problem is after installation on another computer manually I need to copy the PrintAds.htm file to D: directory, instead of that I want to bind the file in project and dynamically pass the url to WebBrowser control. Commented Apr 11, 2014 at 7:46

1 Answer 1

1

So you want to access the HTML file relative to the application root?

In which case can't you just pass something like:

var localURL = Path.Combine(Directory.GetCurrentDirectory(), "PrintAds.htm");
this.webBrowser1.Url = new System.Uri(localURL, System.UriKind.Absolute);

(You need to import the System.IO namespace)

The html file itself should be included within the project, in this example in the root (with a build action as Content)

Please note - Directory.GetCurrentDirectory gets the current working directory - which by default is the application root directory, but can be changed - See the answer at link for alternatives

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

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.