html打开本地文件控件,如何將本地腳本文件添加到WebBrowser控件的HTML中?

博客讨论了在WinForms应用中使用WebBrowser控件加载本地JavaScript文件时遇到的问题。开发者发现直接引用桌面HTML文件可以正常工作,但通过控件加载则无法执行。解决方案包括使用`file://`前缀,修改注册表设置,以及在开发环境中使用Chrome的允许文件访问选项。文章还提到了安全考虑和IE的特性控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This seems really dumb. I've tried a bunch of different ways and it's just not working. I have a WinForms app with a WebBrowser control. If I try with a raw html file on my desktop using the same src string, the src I put together works fine. But plugging the same stuff into the WebBrowser control won't work.

這看起來真的很蠢。我嘗試了一些不同的方法,但它只是不起作用。我有一個帶WebBrowser控件的WinForms應用程序。如果我使用相同的src字符串在我的桌面上嘗試使用原始html文件,我放在一起的src工作正常。但是將相同的東西插入WebBrowser控件將無法正常工作。

Here's my code:

這是我的代碼:

HtmlElementCollection head = this.wbPreview.Document.GetElementsByTagName( "head" );

if (head != null)

{

HtmlElement elm = this.webBrowserControl.Document.CreateElement("script");

string mySource = Environment.CurrentDirectory + @"\MyScriptFile.js";

elm.SetAttribute("src", mySource);

elm.SetAttribute("type", "text/javascript");

((HtmlElement)head[0]).AppendChild(elm);

}

The WebBrowser doesn't get the script. However, if I change "mySource" to an external resource (via http://), it works fine!

WebBrowser無法獲取腳本。但是,如果我將“mySource”更改為外部資源(通過http://),它可以正常工作!

Help!

4 个解决方案

#1

11

i came up on your post, while playing around with things following worked for me:

我找到了你的帖子,同時玩了以下為我工作的東西:

HtmlElementCollection head = webBrowser1.Document.GetElementsByTagName("head");

if (head != null)

{

HtmlElement elm = webBrowser1.Document.CreateElement("script");

elm.SetAttribute("type", "text/javascript");

elm.InnerText = System.IO.File.ReadAllText(Environment.CurrentDirectory + @"\helperscripts.js");

((HtmlElement)head[0]).AppendChild(elm);

}

, so all methods of helperscript.js can be invoked using

,因此可以使用調用helperscript.js的所有方法

webBrowser1.Document.InvokeScript("methodname");

,這里作為腳本調用的參考:如何在WebBrowser控件中注入Javascript?

greetings

#2

4

Try adding file:// to the URL.

嘗試將file://添加到URL。

#3

1

There is a long story about workarounds of that "security fix" from MS. New behavior was implemented starting from IE7. Take a look into "base" tag and IE Feature controls.

關於MS的“安全修復”的解決方法有一個很長的故事。從IE7開始實施了新的行為。查看“基本”標記和IE功能控件。

I did the following:

我做了以下事情:

//TODO: if not mono

var executableFilename = Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location);

var keys = new[] { executableFilename, [vsname]+".vshost.exe" }; //check!

Action SetRegistryKeyOrFail =

(key, val, regStr) =>

{

var reg =

Registry.CurrentUser.CreateSubKey(regStr);

if (reg == null) throw new Exception("Failed registry: " + regStr);

reg.SetValue(key, val);

};

foreach (var key in keys)

{

SetRegistryKeyOrFail(key, 1, @"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BLOCK_LMZ_IMG");

SetRegistryKeyOrFail(key, 0, @"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BLOCK_LMZ_SCRIPT");

}

#4

0

This is because of security reasons. You need a webserver to do that, else you can access any file on a system which would be a big security hole.

這是出於安全原因。您需要一個Web服務器來執行此操作,否則您可以訪問系統上的任何文件,這將是一個很大的安全漏洞。

In developement mode, you can set e.g on chrome:

在開發模式中,您可以在chrome上設置例如:

chrome.exe --allow-file-access-from-files

And you will be able to run your code.

您將能夠運行您的代碼。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值