6

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!

4

2 回答 2

1

我可以看到三种方法来实现这一点:

1:将您需要的文件写入临时区域中的平面文件,导航WebBrowser到html文件,并在页面加载后将其删除

2:正如你所说,一个嵌入式网络服务器 - 也许是HttpListener- 但请注意,这使用 HTTP.SYS,因此需要管理员权限(或者你需要预先打开端口

3:与 1 类似,但使用命名管道服务器来避免写入文件

不得不说,第一个简单很多,需要零配置。

于 2008-11-08T08:39:10.487 回答
0
/// Hi try this may help u.
private string CheckImages(ExtendedWebBrowser browser)
{
      StringBuilder builderHTML = new StringBuilder(browser.Document.Body.Parent.OuterHtml);
      ProcessURLS(browser, builderHTML, "img", "src");                
      ProcessURLS(browser, builderHTML, "link", "href");
      // ext...

      return builderHTML.ToString();

}

private static void ProcessURLS(ExtendedWebBrowser browser, StringBuilder builderHTML, string strLink, string strHref)
{
     for (int k = 0; k < browser.Document.Body.Parent.GetElementsByTagName(strLink).Count; k++)
     {
          string strURL = browser.Document.Body.Parent.GetElementsByTagName(strLink)[k].GetAttribute(strHref);
          string strOuterHTML = browser.Document.Body.Parent.GetElementsByTagName(strLink)[k].OuterHtml;
          string[] strlist = strOuterHTML.Split(new string[] { " " }, StringSplitOptions.None);
          StringBuilder builder = new StringBuilder();
          for (int p = 0; p < strlist.Length; p++)
          {
              if (strlist[p].StartsWith(strHref))                        
                  builder.Append (strlist[p].Contains("http")? strlist[p] + " ":
                      (strURL.StartsWith("http") ?  strHref + "=" + strURL + " ":
                           strHref + "= " + "http://xyz.com" + strURL + " " ));                           
              else
                  builder.Append(strlist[p] + " ");
          }

          builderHTML.Replace(strOuterHTML, builder.ToString());
      }
}
于 2009-09-24T12:44:21.543 回答