How to load HTML / JavaScript from an embedded resource into a winform web browser

I want some JavaScript HTML files to be loaded into a web browser control in a winforms application (v2.0). At runtime, I will not have access to the Internet, so JavaScript and HTML forms will be embedded in the resources.resx file.

1) how can I load an HTML document from a resource (similar to the operation with the file: ///, but it does not load from the file system),

2) How can I declare JavaScript scripts to load? I.e.

<script src=resource.jquery.min.js??? ... /> 

Thanks!

+6
source share
2 answers

To load an HTML document, simply compile the html file as an embedded resource, and then:

 WebBrowser browser = new WebBrowser(); browser.DocumentText = Properties.Resources.<your_html_file>; 

If you really need external .js files, I think you will probably need to make them inline resources. You can then read these resources in a javascript line.

 string GetResourceString(string scriptFile) { Assembly assembly = Assembly.GetExecutingAssembly(); Stream str = assembly.GetManifestResourceStream(scriptFile); StreamReader sr = new StreamReader(str, Encoding.ASCII)); return sr.ReadToEnd(); } 

(Adapted from the answer on this page )

Next, consider the IHTMLScriptElement. As far as I understand, you can use this javascript line and set it as an ITHMLScriptElement text field. See this question

Good luck.

+6
source

Here is the file structure.

enter image description here

I had success doing this:

Set the properties of the html files in my solution as follows:

 Build Action -> Content Copy to Output Directory -> Copy always 

Set the properties of the webBrowser object as follows:

 var myAssembly = System.Reflection.Assembly.GetExecutingAssembly().CodeBase; var path = myAssembly.Substring(0, myAssembly.Length - "MyLib.DLL".Length) + "WebViews/prototype/index.html"; webBrowser.Url = new Uri(path); 
0
source

Source: https://habr.com/ru/post/911990/


All Articles