使用codedom时无法将嵌入资源复制到磁盘

时间:2016-06-11 15:02:36

标签: vb.net resources embedded-resource codedom

我正在运行以下代码,以便在运行时将嵌入式资源复制到磁盘中的vb.net应用程序正常工作(嵌入式资源文件添加到项目资源文件夹中,并在属性中设置为嵌入式资源)

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
Dim Appdatapath As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
CopyResourceToDisk("Myapplicationrootnamespace.resourcefile.xml", Appdatapath & "\Myapp\resourcefile.xml")

但是当我在codedom中尝试相同的逻辑来实现完全相同的事情(在运行生成的可执行文件时将嵌入的资源复制到磁盘)时,它会失败并显示错误:对象引用未设置为对象的实例

这是我尝试这样做的方式:

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()
complierparams.EmbeddedResources.Add(System.Windows.Forms.Application.StartupPath & "\tmp.resources")

以及在运行时编译的实际代码:

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
CopyResourceToDisk("resourcefile.xml", Appdatapath & "\resourcefile.xml")

当我执行动态编译的exe时,它在运行时给出了错误。请帮我。感谢

0 个答案:

没有答案