4

I am trying to produce generic .NET executables that can be customized by changing the embedded resources. Ideally, I would like to be able to do this with a compiled executable, but it would also be acceptable to use some sort of intermediate representation. The key is that I need to be able to do this with a program, not manually.

Consider a C# program that looks something like this:

namespace MyProgram {
    public class Program {
        public static void Main(string[] args)
        {
            using(var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProgram.Resources.foo"))
            {
                // ignore stream == null
                DoStuff(stream);
            }
        }
    }
}

This will work as expected when some resource is embedded and named "foo". However, what I am trying to accomplish is building the program without the resource embedded, and then 'post-processing' the program to embed the resource. In fact, it would be even better if it's possible to both embed a new resource with an arbitrary name and add it to the resource manifest, so that at runtime the program can read the embedded resources and act on them.

Is there a way to programmatically embed new resources into a compiled .NET executable?

4
  • I'm betting the easiest way would be to modify the csproj file on-the-fly and use the compiler. Commented Apr 11, 2018 at 18:20
  • If I edit the csproj to include resources, will that automatically add them to the resource manifest, so that they can be enumerated at runtime? That's not a bad solution, if so... Commented Apr 11, 2018 at 18:23
  • 2
    It is not impossible, but recommended install locations and anti-malware being anti will give you a headache that nobody needs. It just quickly stops making sense to distinguish a file that has an embedded resource that has to be created at runtime from the plain resource trivially been stored in a file. Use System.CodeDom if you want to do this anyway. Commented Apr 11, 2018 at 18:24
  • Does it really need to be embedded? We use server scripts to build and code-sign self-extracting archives that include an application plus dynamically customized additional files. For PHP we use OSSLSignCode, for Tomcat we use some Java code-signing package (I don't know which one). Commented Apr 11, 2018 at 18:30

1 Answer 1

3

Say you have a project called Resources.csproj.

You could copy resources into a specific directory (e.g. Content).

Project directory structure
----------------------------
Properties
  AssemblyInfo.cs
Content
  Item1.png
  Item2.png

You could then dynamically update your csproj file using XDocument or similar to include your content as embedded resources.

<Project>
  <ItemGroup>
    <EmbeddedResource Include="Content\Item1.png" />
    <EmbeddedResource Include="Content\Item2.png" />
  </ItemGroup>
</Project>

You could then compile the project however you like.

msbuild Resources.sln

Your resources should then be available in the output assembly. The advantage is that this approach avoids the overhead of dealing with CodeDom.

Sign up to request clarification or add additional context in comments.

1 Comment

I was hoping to avoid invoking a compiler to build the customized executable, but otherwise this is a great solution. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.