Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to have some HTML files with JavaScript loaded into the web browser control in a winforms (v2.0) application. During execution, I won't have internet access, so JavaScript and HTML forms will be embedded in he resources.resx file.

1) how can I load an HTML document out of the resource (analogous to a file:/// operation, but it isn't being loaded from the file system),

2) how would I declare the JavaScript scripts to be loaded? I.e.,

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

Thanks!

like image 487
SC28 Avatar asked Mar 29 '12 22:03

SC28


1 Answers

To load the HTML document, just compile your html file as embedded resource, and then:

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

If you really need to have external .js files, I think you will probably need to make them embedded resources. Then you can read these resources into a string of javascript.

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 a reply on this page)

From here, look into IHTMLScriptElement. From what I understand, you may be able to use this javascript string and set it as the ITHMLScriptElement's text field. See this question

Good luck.

like image 151
tronbabylove Avatar answered Oct 01 '22 00:10

tronbabylove