Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WebBrowser HTML with references to scripts and images

I'm writing a C# app using the WebBrowser control, and I want all content I display to come from embedded resources - not static local files, and not remote files.

Setting the initial text of the control to an embedded HTML file works great with this code inspired by this post:

browser.DocumentText=loadResourceText("myapp.index.html");

private string loadResourceText(string name)
{
    Assembly assembly = Assembly.GetExecutingAssembly();
    Stream stream = assembly.GetManifestResourceStream(name);
    StreamReader streamReader = new StreamReader(stream);
    String myText = streamReader.ReadToEnd();
    return myText;
}

As good as that is, files referred to in the HTML - javascript, images like <img src="whatever.png"/> etc, don't work. I found similar questions here and here, but neither is asking exactly what I mean, namely referring to embedded resources in the exe, not files.

I tried res://... and using a <base href='..." but neither seemed to work (though I may have not got it right).

Perhaps (following my own suggestion on this question), using a little embedded C# webserver is the only way... but I would have thought there is some trick to get this going?

Thanks!

like image 828
Roark Fan Avatar asked Nov 08 '08 03:11

Roark Fan


1 Answers

I can see three ways to get this going:

1: write the files you need to flat files in the temp area, navigate the WebBrowser to the html file, and delete them once the page has loaded

2: as you say, an embedded web-server - herhaps HttpListener - but note that this uses HTTP.SYS, and so requires admin priveleges (or you need to pre-open the port)

3: like 1, but using named-pipe server to avoid writing a file

I have to say, the first is a lot simpler and requires zero configuration.

like image 154
Marc Gravell Avatar answered Oct 16 '22 16:10

Marc Gravell