Unraveling the confusion about Embedded Resources

前端 未结 2 1641
既然无缘
既然无缘 2020-12-29 10:30

EDIT: Read answer number 1 from Tim Schmelter and then use this question for examples of how to embed resources and access them at runtime.

The subj

相关标签:
2条回答
  • 2020-12-29 10:43

    I assume that Method 1 is adding the files twice.

    • http://www.vbdotnetforums.com/vb-net-general-discussion/42670-visual-basic-net-2008-get-resource-file-io-stream.html#post121923

    At least that is the conclusion of the thread above.

    Quote:

    You went to the Resources page of the project properties and added the files there, right? You then went into the Solution Explorer and change the Build Action of the files to Embedded Resource, right? That's why you were doubling the file size: you were adding each file twice.

    There are two different ways to add resources: on the Resources page of the project properties and in the Solution Explorer. You do NOT do both. If you want to use GetManifestResourcestream then you do NOT use the Resources page. You add the files to the project in the Solution Explorer manually, then you set the Build Action to Embedded Resource.

    In future, do one or the other, not both.

    1. Add a file to the Resources page of the project properties and then access it via My.Resources. This will automatically add the file to the project in the Solution Explorer but the Build Action will be None and it should be left that way.

    2. Add the file to the project in the Solution Explorer by using Add New Item or Add Existing Item. Set the Build Action of the file to Embedded Resource and then access the resource using GetManifestResourceStream.

    0 讨论(0)
  • 2020-12-29 11:07

    Just an update for anyone who wants to use this code. The code actually writes one additional byte to the file due to zero-based declaration of the byte array.

    To get an exact copy of the original file change the code to:

    Using s As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(WindowsApplication1.test.exe)
        Dim bytes(s.Length-1) As Byte
        s.Read(bytes, 0, bytes.Length)
        File.WriteAllBytes(“C:\test.exe”, bytes)
    End Using
    
    0 讨论(0)
提交回复
热议问题