![](/img/trans.png)
[英]Which video formats does the vb.net web browser control support and why won't it play this video even if it works in IE?
[英]Vb.Net (Visual basic), Web Browser Control; make an external file available to pages
我一直在为自定义软件开发“ IDE”,该软件依赖于Web浏览器控件来显示其信息。 该软件使用外部文件“ common.vbs”来访问其网页中的常用功能。 html标记散布在应用程序的各个网页中:
<script LANGUAGE="VBScript" src="common.vbs"></script>
在我的应用程序中,将代码导入资源字符串后,我尝试了以下代码:
Dim sPath As String = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
sPath = Path.Combine(sPath, "common.vbs")
Try
Dim fs As FileStream = File.Create(sPath)
Dim sData As String = My.Resources.common
Dim Info As Byte() = New UTF8Encoding(True).GetBytes(My.Resources.common)
fs.Write(Info, 0, Info.Length)
fs.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
代码没有错误。 当我测试(使用相同的功能)读回文件时,将返回文件的内容; 我知道文件正在写入,但是当任何页面加载了以上HTML标记时,我都会从该页面收到错误消息:
An error has occurred in the script on this page
Line: 0
Char: 0
Error: Script error
Code: 0
URL: about:common.vbs
我尝试“包含”的文件仅包含如下功能:
Function DoHelp
window.external.DoHelp "", "", -1, -1
End Function
Function SQLDate( d)
SQLDate = "'" & DatePart("yyyy", d) & "-" & DatePart("m", d) & "-" & DatePart("d", d) & "'"
End Function
Function MonitorJob( jobId)
on error resume next
window.external.UIControl.MonitorJob jobId, 0
End Function
在应用程序内,加载和使用文件中包含的任何功能都没有问题。 该文件的实际内容来自应用程序,尚未在任何庄园中进行过编辑。 我在应用程序中加载的包含该文件的任何页面都包含错误,包括简单的测试页面。
是否有人对如何使Web浏览器控件识别脚本的scr标记中的文件有任何想法,而该脚本不是托管应用程序加载和提供的实际页面的一部分? 实际上,在源应用程序中编辑所有页面是不可行的。 可能是动态创建的,并且有很多。
我已尝试在此处提供尽可能多的信息,但是如果您需要更多信息,请随时提问!
谢谢,弗雷德
编辑/更新:我尝试将需要“包含”的所有文件写入临时文件夹,将调用HTML文件写入同一文件夹,然后使用文件流加载Web控件。 结果是一样的。
编辑/更新我一定是错了; 将所有“资源文件”写入temp文件夹,然后将生成的HTML页面写入相同的文件,最后将Navigate方法调用到该temp文件夹和文件中,从而使该页面正确加载图像和基础包含的VBS文件。
最后的编辑/更新我花了一些时间才能使所有零件正常工作。 以下是解决问题的答案。
问题的一部分是如何加载Web控件的。 使用文档方法可避免访问文件系统,因为未使用任何基本位置。 为了访问文件系统中的项目,“源页面”必须位于文件系统中。 问题的下一部分是将资源放入文件系统。 下面是我使用的代码。 重要说明:仅出于演示目的,临时路径的代码位于此例程内。 它实际上位于例程之外,以供其他例程访问。
Sub UpdateFiles(sData)
'Write all the dependent files into the working folder.
Dim sLocalPath As String = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString)
Do While Directory.Exists(TempWorkingPath) Or File.Exists(TempWorkingPath)
sLocalPath = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString)
Loop
Directory.CreateDirectory(sLocalPath)
File.WriteAllBytes(Path.Combine(sLocalPath, "common.vbs"), System.Text.Encoding.UTF32.GetBytes(My.Resources.common))
Dim aImageList() As String = Split(My.Resources.M3ImageList, vbCrLf)
Dim sImageList As String = Replace(Join(Split(My.Resources.M3ImageList, vbCrLf), ","), "-", "_")
Dim ResourceSet As Resources.ResourceSet = My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.CurrentCulture, True, True)
For Each Dict As DictionaryEntry In ResourceSet.OfType(Of Object)()
If TypeOf (Dict.Value) Is Drawing.Image Then
If InStr(sImageList, Dict.Key, vbTextCompare) > 0 Then
Dim ImageConverter As New ImageConverter
Dim aBytes() As Byte = ImageConverter.ConvertTo(Dict.Value, GetType(Byte()))
Dim sName As String = Replace(Dict.Key.ToString & ".gif", "_", "-")
File.WriteAllBytes(Path.Combine(sLocalPath, sName), aBytes)
End If
End If
Next
File.WriteAllText(Path.Combine(sLocalPath, "index.html"), sData)
End Sub
我使用了一个单独的资源文件,该文件列出了驱动该例程所需的所有文件。 值得注意的一点是,当我导入所有GIF时,-都被转换为_,从而替换了语句。 我还使用了过滤器(Instr命令),以便仅导出列表中的项目。
我花了很多时间在此上,希望其他人会发现它有用!
弗雷德
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.