How to embed an application manifest in a CodeDom generated assembly

When using CodeDom to generate executables (using CodeCompileUnit.CompileAssemblyFromDom for example), you can easily embed your custom application manifest using the CompilerParameters.CompilerOptions string, like so:

            CompilerParameters cp = new CompilerParameters();
            cp.GenerateExecutable = true;
            cp.GenerateInMemory = false;
            cp.TreatWarningsAsErrors = false;
            cp.OutputAssembly = "RandomFileName.exe";
            cp.CompilerOptions = "/optimize+ /debug- /win32manifest:RandomFileName.exe.manifest";

The relevant option being /win32manifest. Make sure you create the manifest before attempting to compile, obviously.

And use the CompileParameters in CompileAssemblyFromDom();

FYI – There are other ways to do this, but they would involve finding and running some command line utilities. This is by far the easiest, and most elegant way I could find.

Leave a Reply