Show an HTML file of my Project in WebBrowser control at run-time

后端 未结 1 1230
说谎
说谎 2021-01-27 14:17

I want to show a local html file which exists in my project:

The calling file is the HelpFile.cs and in that the form contains the WebBrowser

1条回答
  •  遥遥无期
    2021-01-27 14:57

    A file which exists in your project, lives in a specific location in your machine. But after you distribute the program and run it on user's machine, the file will not exists in the target machine.

    You may want to distribute the file or add it as resource. To solve the problem you can use either of the following solutions:

    • You can copy the file to output directory at build time
    • You can add the file to a resource file like Resources.resx
    • You can make the file as an embedded resource

    Then to show the file, you can use the following methods:

    • Get the file path and call the Navigate method or assign it to Url property
    • Get the resource content and assign it to DocumentText property
    • Get the resource stream and assign it to DocumentStream property

    Copy the file to Output Directory

    To copy the file to output directory at build time:

    • Solution explorer → See properties of your file
    • Set Build Action to Content.
    • Set Copy to Output Directory to Copy always.

    Then the file will be copied to your output directory and you can use it this way:

    var path = System.IO.Path.Combine(Application.StartupPath, "test.html");
    this.webBrowser1.Navigate(path);
    

    Please note, if the file is located under a folder in your project, for example under MyFolder, then it will be copied into a folder with the same name in the output directory of the application:

    var path = System.IO.Path.Combine(Application.StartupPath, "MyFolder", "test.html");
    this.webBrowser1.Navigate(path);
    

    Add the file to a resx resource file like Resources.Resx

    You can add the file to resource file of the project. This way it will be distributed in a resource assembly and you don't need to copy the file to output directory. To do so:

    • Solution explorer → Your project → Properties folder → open Resources.Resx file
    • From toolbar of the designer → Add existing file → Add the html file.

    Then the content of the file will be available through a string property of the Resources. The property name will be same as the file name, for example if the file name is test.html, the property name will be test and You can use it this way:

    this.webBrowser1.DocumentText = Properties.Resources.test;
    

    Please note, for this solution the file doesn't need to be distributed by your project and it will be part of the resource file. However it will be part of your project file.

    Make the file as an embedded resource

    You can make the file as an embedded resource. This way it will be distributed in a resource assembly and you don't need to copy the file to output directory. To do so:

    • Solution explorer → See properties of your file
    • Set Build Action to Embedded Resource.
    • Set Copy to Output Directory to Do not copy.

    Then to use, you need to get the file content from embedded resources. Assuming the file name is "test.html":

    var fileName = "test.html";
    var name = Assembly.GetExecutingAssembly().GetManifestResourceNames()
        .Where(x => x.EndsWith(fileName)).First();
    webBrowser1.DocumentStream = 
        Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
    

    Please note, if you have the file inside a folder like MyFolder in the project, then the filename in above example will be "MyFolder.test.html".

    0 讨论(0)
提交回复
热议问题