vb.netresources

Compiling VB.NET project at runtime with resources


My app bundles a set of VBScripts (UTF8 text files) into a VB.NET assembly. I used to embed the script code into the classes as String members, but this is proving to be a poor design. I want to include them as resources instead. What's the best way to add resources to the CompilerParams?

What I've got so far (excluding all the error checks):

  Dim comParams As New CompilerParameters()
  comParams.OutputAssembly = DLL_Path
  comParams.ReferencedAssemblies.Add("System.dll")
  comParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
  comParams.ReferencedAssemblies.Add("System.Drawing.dll")
  comParams.ReferencedAssemblies.Add("mscorlib.dll")
  comParams.ReferencedAssemblies.Add(RhinoDotNETSDKPath)

  comParams.WarningLevel = 3
  comParams.CompilerOptions = "/target:library /optimize"

  comParams.GenerateExecutable = False
  comParams.GenerateInMemory = False

  Dim iCode As String = Me.DotNETSource

  Dim iProvider As CodeDomProvider = CodeDomProvider.CreateProvider("VB")
  comResult = iProvider.CompileAssemblyFromSource(comParams, New String() {iCode})

The CompilerParameters contain fields for EmbeddedResources and LinkedResources, but from the examples I found so far these seem to pertain only to framework resources???


Solution

  • Have you just tried the EmbeddedResources property?

    Dim resources As new StringCollection
    resources.Add("foo/bar.txt")
    resources.Add("data/file.xml")
    comParams.EmbeddedResources = resources
    

    I haven't checked yet (will do so soon) but I think that should work fine. If it doesn't work for you, please give details as to what goes wrong when you try.

    I believe that if you want to change the name in the assembly, you'd use something like:

    resources.Add("foo/bar.txt,testdata.bar.txt")