如何将程序集清单添加到 .NET 可执行文件? [英] How to add an assembly manifest to a .NET executable?

查看:29
本文介绍了如何将程序集清单添加到 .NET 可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将程序集清单添加到我的 .NET 可执行文件中?

<小时>

程序集清单是一个 XML 文件,它被添加到资源类型为 RT_MANIFEST (24) 的 .NET 可移植可执行文件 (PE) 中.

程序集清单用于声明有关可执行文件的许多内容,例如:

  • 如果我想禁用 DPI 缩放,因为我是一名优秀的开发人员:

    <!-- 我们在 Windows Vista 上具有高 dpi 感知能力 --><asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"><asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings"><dpiAware>真</dpiAware></asmv3:windowsSettings></asmv3:application>

  • 我可以声明我是在 Windows 7 上设计和测试的,我应该继续依赖 Windows 7 中的任何错误

    <兼容性 xmlns="urn:schemas-microsoft-com:compatibility.v1"><应用程序><!--下面的 ID 表示应用程序支持 Windows 7 --><supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/></应用程序></兼容性>

  • 我可以声明我是一个优秀的开发者,不需要文件和注册表虚拟化

    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"><安全><请求的权限><requestedExecutionLevel level="asInvoker" uiAccess="false"/></requestedPrivileges></安全></trustInfo>

  • 我可以声明我依赖于 Microsoft Common Controls 库的特定版本 6:

    <依赖><从属程序集><程序集标识类型=win32"name="Microsoft.Windows.Common-Controls"版本="6.0.0.0"处理器架构=X86"publicKeyToken="6595b64144ccf1df"语言=*"/></dependentAssembly></依赖>

  • 我可以声明我依赖于特定版本的 GDI+:

    <依赖><从属程序集><assemblyIdentity type="win32" name="Microsoft.Windows.GdiPlus" version="1.0.0.0" processorArchitecture="x86"publicKeyToken="6595b64144ccf1df" 语言="*"/></dependentAssembly></依赖>

<小时>

不起作用,因为它嵌入了具有默认设置的清单,而不是我的设置.

  • Manifest下,将组合选项更改为Create application without a manifest:

    不起作用,因为它没有嵌入清单

  • 资源下选择资源文件单选选项:

    不起作用,因为您无法选择程序集清单(或包含程序集清单的资源脚本)

  • 资源下,选择资源文件单选选项,然后输入程序集清单XML文件的路径:

不起作用,因为 Visual Studio 在提供程序集清单时会卡住:

  • 资源下,选择资源文件单选选项,然后输入资源脚本文件的路径:

不起作用,因为 Visual Studio 在提供资源脚本时会卡住:

  • AssemblyManifest.xml 添加到我的项目中,然后在 Manifest 组合框中查找:

    不起作用,因为程序集清单文件未作为选项列出

<块引用>

我还有很多其他的东西可以继续截图(向解决方案添加一个 .rc 文件,在下拉列表中查找它,选择无清单"并更改 wumpa.rc 构建操作对于各种事情,使用单独的资源编译器手动或预构建/msbuild 步骤构建 .rc 文件,然后选择该 .res 文件作为我的资源).我将停止为我的问题添加额外的内容,并希望得到答案.

解决方案

如果您想将自定义信息添加到应用程序的清单中,您可以按照以下步骤操作:

  1. 右键单击解决方案资源管理器中的项目.
  2. 点击添加新项目".
  3. 选择应用程序清单文件".

这会将名为 app.manifest 的文件添加到您的项目中,您可以根据需要打开和修改该文件.

<小时>

类似的步骤,附截图,摘自 在 MSDN 上将托管应用程序声明为 DPI 感知:

  1. 在解决方案资源管理器中,右键单击您的项目,指向添加,然后单击新建项目.

  2. 添加新项目对话框中,选择应用程序清单文件,然后点击添加.出现 app.manifest 文件.

  3. 将以下文本复制并粘贴到 app.manifest 文件中,然后保存.

    <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/><!-- 禁用文件和注册表虚拟化.--><trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"><安全><requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"><requestedExecutionLevel level="asInvoker" uiAccess="false"/><!-- <requestedExecutionLevel level="asInvoker" uiAccess="false"/><requestedExecutionLevel level="requireAdministrator" uiAccess="false"/><requestedExecutionLevel level="highestAvailable" uiAccess="false"/>--></requestedPrivileges></安全></trustInfo><!-- 我们在 Windows Vista 上具有高 dpi 意识--><asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"><asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings"><dpiAware>真</dpiAware></asmv3:windowsSettings></asmv3:application><!-- 声明我们设计用于 Windows Vista 和 Windows 7--><兼容性 xmlns="urn:schemas-microsoft-com:compatibility.v1"><应用程序><!--下面的ID表示对Windows Vista的应用程序支持--><supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/><!--下面的 ID 表示应用程序支持 Windows 7 --><supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/></应用程序></兼容性><!-- 为 Windows 通用控件和对话框(Windows XP 及更高版本)启用主题--><依赖><从属程序集><程序集标识类型=win32"name="Microsoft.Windows.Common-Controls"版本="6.0.0.0"处理器架构=*"publicKeyToken="6595b64144ccf1df"语言=*"/></dependentAssembly></依赖></asmv1:assembly>

  4. 在解决方案资源管理器中,右键单击项目,然后单击属性以验证app.manifest使用.

  5. 您的应用程序现在显示为专为 Windows 设计"的要求,并且是

    • 禁用文件和注册表虚拟化
    • 禁用应用程序的 DWM 缩放
    • 宣布您在 Windows 7 和 Windows Vista 上进行设计和测试
    • 依赖于通用控件库版本 6(允许通用控件使用视觉样式)

How can i add an assembly manifest to my .NET executable?


An assembly manifest is is an XML file that is added to a .NET portable executable (PE) with resource type RT_MANIFEST (24).

Assembly manifests are used to declare a number of things about the executable, e.g.:

  • If i want to disable DPI-scaling because i am a good developer:

    <!-- We are high-dpi aware on Windows Vista -->
    <asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
       <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
          <dpiAware>true</dpiAware>
       </asmv3:windowsSettings>
    </asmv3:application>
    

  • i can declare that i was designed and tested on Windows 7, and i should continue to depend on any bugs in Windows 7

    <!-- We were designed and tested on Windows 7 -->
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
       <application>
          <!--The ID below indicates application support for Windows 7 -->
          <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
       </application>
    </compatibility>
    

  • i can declare that i am a good developer, and don't need file and registry virtualization

    <!-- Disable file and registry virtualization -->
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
       <security>
          <requestedPrivileges>
             <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
          </requestedPrivileges>
       </security>
    </trustInfo>
    

  • i can declare that i depend on a particular version 6 of the Microsoft Common Controls library:

    <!-- Dependency on Common Controls version 6 -->
    <dependency>
       <dependentAssembly>
          <assemblyIdentity
                type="win32"
                name="Microsoft.Windows.Common-Controls"
                version="6.0.0.0"
                processorArchitecture="X86"
                publicKeyToken="6595b64144ccf1df"
                language="*"/>
       </dependentAssembly>
    </dependency>
    

  • i can declare that i depend on a particular version of GDI+:

    <dependency>
       <dependentAssembly>
          <assemblyIdentity type="win32" name="Microsoft.Windows.GdiPlus" version="1.0.0.0" processorArchitecture="x86"
                publicKeyToken="6595b64144ccf1df" language="*" />
       </dependentAssembly>
    </dependency>
    


In the olden days, we would create a resource script file (*.rc), e.g.:

wumpa.rc

   1    24    AssemblyManifest.xml

add that file to the project, and the compiler would compile the .rc file; including resources in the final executable image.

Except Visual Studio 2010 doesn't seem to have a way to add a resource script file to a project.

How do i add a resource script to a project in Visual Studio 2010?

How do i add an assembly manifest to a project in Visual Studio 2010?

Note: Any solution must work in an environment with source control and multiple developers (e.g. hard-coded paths to probably not installed binaries will break the build and not work).

Bonus Chatter


Update: Michael Fox suggests that the project properties dialog can be used to include an assembly manifest, but he doesn't indicate where:


Update: Things I've tried:

  • From the project properties screen, select Application. Select radio option Icon and Manifest. Under Manifest leave the default option of Embed manifest with default settings:

Doesn't work because it embeds a manifest with default settings, rather than my settings.

  • Under Manifest, change the combo option to Create application without a manifest:

    Doesn't work because it embeds no manifest

  • Under Resources select the Resource File radio option:

    Doesn't work because you cannot select an assembly manifest (or a resource script that includes an assembly manifest)

  • Under Resources, select the Resource File radio option, then enter the path to an assembly manifest XML file:

Doesn't work because Visual Studio chokes when presented with an assembly manifest:

  • Under Resources, select the Resource File radio option, then enter the path to a resource script file:

Doesn't work because Visual Studio chokes when presented with a resource script:

  • Add the AssemblyManifest.xml to my project, then look for it in the Manifest combo box:

    Doesn't work because the Assembly Manifest file isn't listed as an option

i have a dozen other things i can keep screenshotting (add a .rc file to the solution, look for it in the dropdown, select "no manifest" and change the wumpa.rc build action to various things, build the .rc file using a separate resource compiler, either manually, or a pre-build/msbuild step, and select that .res file as my resource). i'll stop adding extra bulk to my question and hope for an answer.

解决方案

If you want to add custom information to your application's manifest, you can follow these steps:

  1. Right-click on the project in the Solution Explorer.
  2. Click "Add New Item".
  3. Select "Application Manifest File".

This adds a file named app.manifest to your project, which you can open and modify as desired.


Similar steps, with screenshots, lifted from Declaring Managed Applications As DPI-Aware on MSDN:

  1. In the Solution Explorer, right-click on your project, point to Add, and then click New Item.

  2. In the Add New Item dialog box, select Application Manifest File, and then click Add. The app.manifest file appears.

  3. Copy and paste the following text into the app.manifest file and then save.

    <?xml version="1.0" encoding="utf-8"?>
    <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" 
                    xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" 
                    xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" 
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
      <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
    
        <!-- Disable file and registry virtualization. -->
        <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
          <security>
            <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
             <requestedExecutionLevel level="asInvoker" uiAccess="false" />
             <!--  <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
                   <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
                   <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />
             -->
            </requestedPrivileges>
          </security>
        </trustInfo>
    
        <!-- We are high-dpi aware on Windows Vista -->
        <asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
          <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
            <dpiAware>true</dpiAware>
          </asmv3:windowsSettings>
        </asmv3:application>
    
        <!-- Declare that we were designed to work with Windows Vista and Windows 7-->
        <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
          <application>
            <!--The ID below indicates application support for Windows Vista -->
            <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
            <!--The ID below indicates application support for Windows 7 -->
            <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
          </application>
        </compatibility>
    
        <!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
        <dependency>
          <dependentAssembly>
            <assemblyIdentity
                type="win32"
                name="Microsoft.Windows.Common-Controls"
                version="6.0.0.0"
                processorArchitecture="*"
                publicKeyToken="6595b64144ccf1df"
                language="*"
            />
          </dependentAssembly>
        </dependency>
    
      </asmv1:assembly>
    

  4. In the Solution Explorer, right-click on the project, and then click Properties to verify that the app.manifest is used.

  5. Your application is now manifested as required to be "designed for Windows", and is

    • disables file and registry virtualization
    • disables DWM scaling of applications
    • announces that you were designed and tested on Windows 7 and Windows Vista
    • takes a dependency on Common Controls library version 6 (enabling the use of visual styles by the common controls)

这篇关于如何将程序集清单添加到 .NET 可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆