C#web浏览器的HTML引用脚本和图像 [英] C# WebBrowser HTML with references to scripts and images

查看:433
本文介绍了C#web浏览器的HTML引用脚本和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写使用WebBrowser控件一个C#应用程序,我想我显示来自嵌入的资源,所有的内容 - 而不是静态的本地文件,而不是远程文件

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.

设置控件的初始文本嵌入HTML文件与此code灵感的这个帖子

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;
}

如说是在HTML的简称,文件 - JavaScript中,如&LT图像; IMG SRC =whatever.png/&GT; 等,不要科技工作。我发现了类似的问题,<一个href="http://stackoverflow.com/questions/72103/how-do-i-reference-a-local-resource-in-generated-html-in-winforms-webbrowser-co#273840">here和<一href="http://stackoverflow.com/questions/153748/webbrowser-control-from-net-how-to-inject-javascript">here,但也不是问的完全的我是什么意思,也就是指的嵌入式的资源的EXE,而不是文件。

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.

我试过水库:// ... 和使用&LT;基地的HREF =...但既不似乎工作(虽然我可能还没有得到它的权利)。

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

也许(以下我自己的建议在<一个href="http://stackoverflow.com/questions/72103/how-do-i-reference-a-local-resource-in-generated-html-in-winforms-webbrowser-co#273840">this问题),使用一个小的嵌入式C#的Web服务器是唯一的方法......但是我还以为有一些窍门得到这个打算?

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?

谢谢!

推荐答案

我可以看到三种方式来获得这种的:

I can see three ways to get this going:

1:写你需要平面文件在临时区域中的文件,浏览<$​​ C $ C> web浏览器的HTML文件,一旦在页面加载删除它们。

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:正如你所说,一个嵌入式网络服务器 - herhaps 的HttpListener - 但请注意,这使用HTTP.SYS,所以需要管理priveleges(或者你需要 pre-开放的端口

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:像1,但使用命名管道服务器,以避免写入文件

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.

这篇关于C#web浏览器的HTML引用脚本和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆