Fred Kerber Fred Kerber - 1 year ago 94
HTML Question

Vb.Net (Visual basic), Web Browser Control; make an external file available to pages

I've been developing an "IDE" for custom software that relies on a web browser control to display it's information. The software uses an external file "common.vbs" to access common functions within it's web pages. Sprinkled throughout various web pages within the application is the html tag:

<script LANGUAGE="VBScript" src="common.vbs"></script>

In my application I've tried the following code after importing the code into a resource string:

Dim sPath As String = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
sPath = Path.Combine(sPath, "common.vbs")
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)
Catch ex As Exception
End Try

The code doesn't error. When I test (withing the same function) reading the file back, it's contents are returned; I know the file is being written, but when the any of the pages load with the above HTML tags, I get an error from the page:

An error has occurred in the script on this page
Line: 0
Char: 0
Error: Script error
Code: 0
URL: about:common.vbs

The file I'm trying to "include" contains only functions like:

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

Within the application, there are no problems loading and using any of the functions contained within the file. The actual contents of the file is from the application and hasn't been edited in any manor. Any page that I load with my application that has the file included errors out, including simple test pages.

Does anyone have any ideas on how to get the web browsers control recognize a file from within a script's scr tag that's not part of the actual page being loaded and supplied from the hosting application? Editing all of the pages within the source application really isn't an option; may are dynamically created and there is a large number of them.

I've tried to include as much information as I can here, but if you need more info, please feel free to ask!

Thanks, Fred

I've tried writing all of the files I need to "include" into a temp folder, write the calling HTML file to the same folder, then load the web control using a file stream. The results are the same.

I must have things wrong; Writing all of the "resource Files" into the temp folder, then writing the resulting HTML page into the same file and finally calling the Navigate method to that temp folder and file results in the page properly loading images and the underlying included VBS file.

It took me a while to get all of the parts working correctly. Below is the answer that solved the issue.

Answer Source

Part of the problem is how the web control is loaded. Using the document methods circumvents accessing the file system because no base location is used. In order to access items in the file system, the "source page" needs to be in the file system. The next part of the problem was getting the resources into the file system. Below is the code that I used. Important note: for demonstration reasons only, the code for the Temp Path is located inside this routine. It actually resides out side the routine for access in other routines.

    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)
      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
      File.WriteAllText(Path.Combine(sLocalPath, "index.html"), sData)
  End Sub

I used a separate resource file that listed all of the files I needed to drive this routine. One point of note, when I imported all of the GIF's, the -'s were converted into _'s, thus the replace statements. I also used a filter (the Instr command) so that only the items in the list were exported.

I spent a lot of time on this, and I hope others will find it useful!


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download