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.

Ahmed Syed
  • 1,179
  • 2
  • 18
  • 44
  • Does it matter if the html file is only in your project folder? –  Apr 11 '14 at 07:37
  • @Zekth I didn't get it! – Ahmed Syed Apr 11 '14 at 07: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. –  Apr 11 '14 at 07: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. – Ahmed Syed Apr 11 '14 at 07:46

1 Answers1

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

Community
  • 1
  • 1
James S
  • 3,558
  • 16
  • 25