Is there a way to conditionally embed resources into a .NET project? I.e. if I have defined INCLUDETHIS then I want a certain large file embedded into the dll, otherwise I do not want it embedded. I know I can make two projects to do this, but I'm hoping to do it all in one project. The code that uses the file is easily conditioned with a #if INCLUDETHIS ... #endif, but I don't even want the file in the dll otherwise as it contains sensitive information and is only used internally.
Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.
Embedded files are called as Embedded Resources and these files can be accessed at runtime using the Assembly class of the System.Reflection namespace. This article will illustrate, how to read and display an embedded Text file and Image file in Windows Application (Windows Forms) in C# and VB.Net.
This did the trick. You can edit your csproj by right clicking in Visual Studio on the project and selecting "edit".
<Choose>
<When Condition=" '$(Configuration)'!='ReleaseExternal' And '$(Platform)'=='x86' ">
<ItemGroup>
<EmbeddedResource Include="file.dll">
<Link>Resources\Other\file.dll</Link>
</EmbeddedResource>
</ItemGroup>
</When>
</Choose>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With