Question unable to copy embedded resources to disk when using codedom

Joined
Apr 28, 2016
Messages
5
Programming Experience
1-3
I am running the below code to copy embedded resource to disk at run time in the vb.net application which works fine (the embedded resource file is added in the project resources folders and set as embedded resource in the properties):

VB.NET:
Expand Collapse Copy
Public Function CopyResourceToDisk(ByVal ResourceName As String, ByVal FileToCopyTo As String) As Boolean

        Dim str As System.IO.Stream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(ResourceName)
        Dim RF As New System.IO.FileStream(FileToCopyTo, IO.FileMode.Create)

        Dim byt(str.Length) As Byte

        str.Read(byt, 0, str.Length)
        RF.Write(byt, 0, byt.Length - 1)
        RF.Flush()
        RF.Close()

        RF = Nothing
    End Function

VB.NET:
Expand Collapse Copy
Dim Appdatapath As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

VB.NET:
Expand Collapse Copy
 CopyResourceToDisk("Myapplicationrootnamespace.resourcefile.xml", Appdatapath & "\Myapp\resourcefile.xml")

But when I try the same logic in the codedom to achieve exactly same thing (copy embedded resource to disk when running the generated executable) it fails with an error : Object reference not set to an instance of an object

This is the way I am trying to do it:

VB.NET:
Expand Collapse Copy
Dim filebyte As Byte() = IO.File.ReadAllBytes("full path of resourcefile.xml")

        Dim writer As New System.Resources.ResourceWriter("tmp.resources")
        writer.AddResource("resourcefile.xml", filebyte)
        writer.Generate()
        writer.Close()

VB.NET:
Expand Collapse Copy
complierparams.EmbeddedResources.Add(System.Windows.Forms.Application.StartupPath & "\tmp.resources")

and in the actual code which gets compiled at run time:

VB.NET:
Expand Collapse Copy
Public Function CopyResourceToDisk(ByVal ResourceName As String, ByVal FileToCopyTo As String) As Boolean

        Dim str As System.IO.Stream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(ResourceName)
        Dim RF As New System.IO.FileStream(FileToCopyTo, IO.FileMode.Create)

        Dim byt(str.Length) As Byte

        str.Read(byt, 0, str.Length)
        RF.Write(byt, 0, byt.Length - 1)
        RF.Flush()
        RF.Close()

        RF = Nothing
    End Function

VB.NET:
Expand Collapse Copy
CopyResourceToDisk("resourcefile.xml", Appdatapath & "\resourcefile.xml")

Please help!! :blue:
 
Last edited:
Check the resource names:
Dim names = Me.GetType.Assembly.GetManifestResourceNames()
 
Back
Top