How can I embed an configuration-specific manifest

2020-07-25 08:39发布

问题:

I'd like to embed a platform-specific manifest file in my C# app. Is there an easy way to do this in Visual Studio 2008 without using some external script called after the build is finished?

In creating a C# app, the configuration and platform dropdowns are disabled for the Application tab, preventing me from selecting an architecture or configuration specific manifest to embed to that app. I'm forced to use one manifest for the whole application.

I thought of using a Post Build event to call a little script to embed the right manifest based on the $(Configuration) macro variable, and it works, but I want to do this without calling into some other script and I want to know if it's possible to do without using Build Events.

Can I embed the x86 and x64 assembly references in the same manifest file and the run time will just load the correct one?

回答1:

You simply need to add the app.manifest file. Right-click the project, select "Add" -> "New Item...". Select the "General" entry on the left tree view under "Visual C# Items". From the "Templates" list, locate and select the item call "Application Manifest File". Click Add.

Now you have a manifest, now let's make it platform specific. Right-click the project and select "Unload Project". Right-click again and select "Edit {project name}.csproj".

Locate the first configuration section, at the end you should find...

<PropertyGroup>
    ...
    <ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

You need to move the ApplicationManifest element into the appropriate configuration section for the platform configuration. You may even need to add a section to the xml as I did here for AnyCPU:

<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
    <ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

When your finished, save the file, right-click the project, and select "Reload Project".