views:

2741

answers:

3

Hello everyone. I am trying to create a form with webbrowser control. I am trying to open a local html file in the webbrowser control. I have the html file in Help_Print folder, so I am using the code below to specify the Url of the webbrowser.

wbPrint.Url = new Uri("file:///" + Application.StartupPath + "\\Help_Print\\help.html");

When the form shows the webbrowser control has an error "This program cannot display the webpage". I have checked the file and folder location.

But when I try this:

wbPrint.Url = new Uri("file:///" + Application.StartupPath + "/help.html");

after copying the html file to the application startup location it works properly.

Can anyone please explain why is this happening, as I want to keep all html file in a separate folder.

+1  A: 

Don't use black-slashes in file urls. See the bizarre and unhappy story of file urls.

jeffamaphone
Thanks for your prompt reply. Even if I use slash:wbPrint.Url = new Uri("file:///" + Application.StartupPath + "/Help_Print/help.html");I get the same error.
kobra
Perhaps you have a permissions issue on the Help_Print folder?
jeffamaphone
I am sure there is no permission issue, because I am able to open the file using:System.Diagnostics.Process.Start(".\\Print_Help\\help.html");form another form. And also if I place the file in the parent folder of Help_Print I am able to see the file in webbrowser control.
kobra
Try using the Navigate method?
jeffamaphone
Thanks for your suggestion. I tried this:String sitePath = "file:///" + Application.StartupPath +"\\Help_Print\\help.html";wbPrint.Navigate(sitePath);But still getting the same error.
kobra
A: 

Hi! I found a solution to my problem even-though I am not sure why my previous code was not working. I changed two thing:

(i) I remove Uri, as I was suggested in answer to a different question of mine, that for local html files I am not required to use Uri. So here is the code that work.

        String sitePath = null;
        try
        {
            sitePath = Application.StartupPath + @"\Print_Help\help.html";
            wbHelp.Navigate(sitePath);
        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.ToString() + "\nSite Path: " + sitePath);
            return false;
        }
        return true;

(ii) The other thing I did was to create the Print_Help folder manually. Previously it was created when I Build my Project as the html file was marked the property Copy to Output Directory as Copy Always.

I think the second change has more to do with my problem solution than the first. Please comment if you understand the logic.

kobra
A: 

use syntax: file://c:/xyz.html

Ayush