使用配置文件配置应用Configuring Apps by using Configuration Files

.NET Framework 通过配置文件为开发人员和管理员提供了对应用程序运行方式的控制权和灵活性。The .NET Framework, through configuration files, gives developers and administrators control and flexibility over the way applications run. 配置文件是可以按需要更改的 XML 文件。Configuration files are XML files that can be changed as needed. 管理员能够控制应用程序可以访问哪些受保护的资源,应用程序将使用哪些版本的程序集,以及远程应用程序和对象位于何处。An administrator can control which protected resources an application can access, which versions of assemblies an application will use, and where remote applications and objects are located. 开发人员可以将设置置于配置文件中,从而没有必要在每次设置更改时重新编译应用程序。Developers can put settings in configuration files, eliminating the need to recompile an application every time a setting changes. 本节说明可以对什么进行配置以及为什么对应用程序进行配置会有用。This section describes what can be configured and why configuring an application might be useful.

备注

托管代码可以使用 System.Configuration 命名空间中的类从配置文件中读取设置,但不向这些文件写入设置。Managed code can use the classes in the System.Configuration namespace to read settings from the configuration files, but not to write settings to those files.

本主题描述配置文件的语法,并提供有关三种配置文件的信息:计算机配置文件、应用程序配置文件和安全配置文件。This topic describes the syntax of configuration files and provides information about the three types of configuration files: machine, application, and security.

配置文件格式Configuration File Format

配置文件包含元素,它们是用来设置配置信息的逻辑数据结构。Configuration files contain elements, which are logical data structures that set configuration information. 在配置文件内,使用标记来标记元素的开头和结尾。Within a configuration file, you use tags to mark the beginning and end of an element. 例如,<runtime> 元素包括<runtime>子元素</runtime>For example, the <runtime> element consists of <runtime>child elements</runtime>. 空元素将写为 <runtime/><runtime></runtime>An empty element would be written as <runtime/> or <runtime></runtime>.

与所有 XML 文件一样,配置文件中的语法区分大小写。As with all XML files, the syntax in configuration files is case-sensitive.

使用预定义特性来指定配置设置,这些特性是元素开始标记内的名称/值对。You specify configuration settings using predefined attributes, which are name/value pairs inside an element's start tag. 下面的示例为 <codeBase> 元素指定两个特性(versionhref),该元素指定运行时在何处可以找到程序集(有关详细信息,请参阅指定程序集的位置)。The following example specifies two attributes (version and href) for the <codeBase> element, which specifies where the runtime can locate an assembly (for more information, see Specifying an Assembly's Location).

<codeBase version="2.0.0.0"  
          href="http://www.litwareinc.com/myAssembly.dll"/>  

计算机配置文件Machine Configuration Files

计算机配置文件 Machine.config 包含应用于整个计算机的设置。The machine configuration file, Machine.config, contains settings that apply to an entire computer. 此文件位于 %runtime install path%\Config 目录中。This file is located in the %runtime install path%\Config directory. Machine.config 包含整个计算机范围内的程序集绑定、内置远程处理信道和 ASP.NET 的配置设置。Machine.config contains configuration settings for machine-wide assembly binding, built-in remoting channels, and ASP.NET.

配置系统首先查看计算机配置文件,查找 <appSettings> 元素,然后查看开发人员可能定义的其他配置节。The configuration system first looks in the machine configuration file for the <appSettings> element and other configuration sections that a developer might define. 然后查看应用程序配置文件。It then looks in the application configuration file. 为使计算机配置文件可管理,最好将这些设置放在应用程序配置文件中。To keep the machine configuration file manageable, it is best to put these settings in the application configuration file. 但是,将这些设置放在计算机配置文件中可以使系统更易维护。However, putting the settings in the machine configuration file can make your system more maintainable. 例如,如果有第三方组件,且客户端和服务器应用程序同时使用该组件,那么将该组件的设置放在一个位置更方便。For example, if you have a third-party component that both your client and server application uses, it is easier to put the settings for that component in one place. 在这种情况下,计算机配置文件是存放设置的合适位置,这样就不会将相同的设置放在两个不同的文件中。In this case, the machine configuration file is the appropriate place for the settings, so you don't have the same settings in two different files.

备注

使用 XCOPY 部署应用程序将不会复制计算机配置文件中的设置。Deploying an application using XCOPY will not copy the settings in the machine configuration file.

若要深入了解公共语言运行时如何使用计算机配置文件进行程序集绑定,请参阅运行时如何定位程序集For more information about how the common language runtime uses the machine configuration file for assembly binding, see How the Runtime Locates Assemblies.

应用程序配置文件Application Configuration Files

应用程序配置文件包含应用特定的设置。An application configuration file contains settings that are specific to an app. 该文件包含公共语言运行时读取的配置设置(如程序集绑定策略、远程处理对象等)以及应用可以读取的设置。This file includes configuration settings that the common language runtime reads (such as assembly binding policy, remoting objects, and so on), and settings that the app can read.

应用程序配置文件的名称和位置取决于应用的主机,可以是下列项之一:The name and location of the application configuration file depend on the app's host, which can be one of the following:

  • 可执行文件承载的应用。Executable–hosted app.

    这些应用具有两个配置文件:一个开发人员在开发过程中修改的源配置文件,一个随应用一起分发的输出文件。These apps have two configuration files: a source configuration file, which is modified by the developer during development, and an output file that is distributed with the app.

    在 Visual Studio 中进行开发时,将应用的源配置文件放置在项目目录中,并将其“复制到输出目录”属性设置为“始终复制”或“如果较新则复制”。When you develop in Visual Studio, place the source configuration file for your app in the project directory and set its Copy To Output Directory property to Copy always or Copy if newer. 配置文件的名称是带 .config 扩展名的应用名。The name of the configuration file is the name of the app with a .config extension. 例如,名为 myApp.exe 的应用应具有名为 myApp.exe.config 的源配置文件。For example, an app called myApp.exe should have a source configuration file called myApp.exe.config.

    Visual Studio 会自动将源配置文件复制到已编译程序集所在的目录中,以便创建随应用程序一起部署的输出配置文件。Visual Studio automatically copies the source configuration file to the directory where the compiled assembly is placed to create the output configuration file, which is deployed with the app. 在某些情况下,Visual Studio 可能修改输出配置文件;有关详细信息,请参阅重定向程序集版本文章中的重定向应用级别的程序集版本一节。In some cases, Visual Studio may modify the output configuration file; for more information, see the Redirecting assembly versions at the app level section of the Redirecting Assembly Versions article.

  • ASP.NET 承载的应用。ASP.NET-hosted app.

    有关 ASP.NET 配置文件的详细信息,请参阅ASP.NET 配置设置For more information about ASP.NET configuration files, see ASP.NET Configuration Settings.

  • Internet Explorer 承载的应用。Internet Explorer-hosted app.

    如果 Internet Explorer 中承载的应用具有配置文件,则该文件的位置是在 <link> 标记中使用以下语法指定的:If an app hosted in Internet Explorer has a configuration file, the location of this file is specified in a <link> tag with the following syntax:

    <link rel="ConfigurationFileName" href="location"><link rel="ConfigurationFileName" href="location">

    在该标记中,location 是指向该配置文件的 URL。In this tag, location is a URL to the configuration file. 这将设置应用基。This sets the app base. 配置文件必须位于应用所在的网站上。The configuration file must be located on the same website as the app.

安全配置文件Security Configuration Files

安全配置文件包含有关与策略级别关联的代码组层次结构和权限集的信息。Security configuration files contain information about the code group hierarchy and permission sets associated with a policy level. 强烈建议使用代码访问安全性策略工具 (Caspol.exe) 修改安全策略,确保策略更改不会损坏安全配置文件。We strongly recommend that you use the Code Access Security Policy tool (Caspol.exe) to modify security policy to ensure that policy changes do not corrupt the security configuration files.

备注

从 .NET Framework 4 开始,安全配置文件仅在安全策略已更改时存在。Starting with the .NET Framework 4, the security configuration files are present only if security policy has been changed.

安全配置文件位于以下位置:The security configuration files are in the following locations:

  • 企业策略配置文件:%runtime-install-path%\Config\Enterprisesec.configEnterprise policy configuration file: %runtime-install-path%\Config\Enterprisesec.config

  • 计算机策略配置文件:%runtime-install-path%\Config\Security.configMachine policy configuration file: %runtime-install-path%\Config\Security.config

  • 用户策略配置文件:%USERPROFILE%\Application data\Microsoft\CLR security config\vxx.xx\Security.configUser policy configuration file: %USERPROFILE%\Application data\Microsoft\CLR security config\vxx.xx\Security.config

本节内容In This Section

如何:使用 DEVPATH 查找程序集How to: Locate Assemblies by Using DEVPATH
描述如何指示运行时在搜索程序集时使用 DEVPATH 环境变量。Describes how to direct the runtime to use the DEVPATH environment variable when searching for assemblies.

重定向程序集版本Redirecting Assembly Versions
描述如何指定程序集的位置以及要使用程序集的哪一版本。Describes how to specify the location of an assembly and which version of an assembly to use.

指定程序集的位置Specifying an Assembly's Location
描述如何指定运行时应搜索程序集的位置。Describes how to specify where the runtime should search for an assembly.

配置加密类Configuring Cryptography Classes
描述如何将算法名称映射到加密类,以及如何将对象标识符映射到加密算法。Describes how to map an algorithm name to a cryptography class and an object identifier to a cryptography algorithm.

如何:创建发布者策略How to: Create a Publisher Policy
描述应当在何时以及如何添加发行者策略文件,以指定程序集重定向和基本代码设置。Describes when and how you should add a publisher policy file to specify assembly redirection and code base settings.

配置文件架构Configuration File Schema
描述架构的层次结构:启动、运行时、网络和其他配置设置类型。Describes the schema hierarchy for startup, runtime, network, and other types of configuration settings.

请参阅See also