使用組態檔設定應用程式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.

注意

Managed 程式碼可以使用 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. 在組態檔內,您要使用標記 (Tag) 來標記項目的開頭和結尾。Within a configuration file, you use tags to mark the beginning and end of an element. 例如,<runtime> 項目是由 <runtime>child elements</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.

如需 Common Language Runtime 如何使用電腦組態檔執行組件繫結的詳細資訊,請參閱執行階段如何找出組件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.

應用程式組態檔的名稱和位置取決於應用程式的主應用程式 (Host),可以是下列其中一個: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 Configuration SettingsFor 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