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):

Code:
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
Code:
Dim Appdatapath As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Code:
 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:

Code:
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()
Code:
complierparams.EmbeddedResources.Add(System.Windows.Forms.Application.StartupPath & "\tmp.resources")
and in the actual code which gets compiled at run time:

Code:
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
Code:
CopyResourceToDisk("resourcefile.xml", Appdatapath & "\resourcefile.xml")
Please help!! :blue:
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,412
Location
Norway
Programming Experience
10+
Check the resource names:
Dim names = Me.GetType.Assembly.GetManifestResourceNames()
 
Top Bottom