In Visual Studio 2010, I am including a zip file into my executable as an Embedded Resource and extracting it for use at runtime. Such a file cannot be included as a file resource (text or binary) because a file resource is always linked as a separate item, which is not what I want. Consequently I cannot reference the file in my code through Properties.Resources
.
To extract the zip file, I have to hardcode its name in my code as follows:
stream = Assembly.GetEntryAssembly().GetManifestResourceStream("myembeddedfile.zip");
I noticed that the zip file is referenced in the .csproj file as follows:
<ItemGroup>
<EmbeddedResource Include="myembeddedfile.zip" />
<None Include="packages.config" />
</ItemGroup>
However, I don't think that the above can be referenced from code. (I know about the None
usage from SO question #1060870.)
Is there any way I can include this file as a bonafide project/solution item and prevent having to hardcode the filename?
EDIT:
A non-hardcoded way to reference the file in code would be something like the following:
GetManifestResourceStream(Properties.Resources.EmbeddedFile);
But as I have mentioned, the above is not possible because an embedded resource is not included in the Properties.Resources
object.
EDIT AFTER FOLLOW-UP EDIT BY DRapp TO HIS ACCEPTED ANSWER:
The scheme involving enums by DRapp below is the ultimate solution to this problem, with one modification: when there are multiple resource files being embedded, the resource filenames will be returned by GetManifestResourceNames()
in alphabetical order. Therefore, the enum
names must be arranged accordingly. Before the following snapshot taken during debugging, the three embedded files were added manually in the following sequence: 1_FIRST, 3_THIRD, 2_SECOND
, but as can be observed, the internal storage is alphabetical:
For the above example with three files, the corresponding enum
would be defined as:
public enum MyResources
{
EmptyZipTest_1_FIRST_zip,
EmptyZipTest_2_SECOND_zip,
EmptyZipTest_3_THIRD_zip
}