Share via


HOW TO:以程式設計的方式檢視繼承的和本機的組態設定

更新:2007 年 11 月

因為每個 ASP.NET 應用程式都從根 Web.config 檔案繼承預設組態設定,所以只需要為覆寫預設值的設定建立 Web.config 檔案。如果階層架構中有其他的 Web.config 檔,您就無法知道應用程式所繼承的預設設定,因此也無法判斷要覆寫的設定。

這個範例會使用非靜態的方法取得組態資料,可以讓您從任何應用程式擷取組態資訊。如果您要從程式碼所在的應用程式中取得組態資訊,請使用靜態方法,這樣會較快處理。如需詳細資訊,請參閱 ASP.NET 組態 API 概觀中的<使用本機和遠端組態設定>章節。

範例

下列程式碼範例會從 [預設的網站] 中,取得名為 MyApp 之 ASP.NET 應用程式的所有組態設定,然後將設定寫入 XML 檔。

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Management
Imports System.Configuration
Imports System.Web.Configuration

Namespace SamplesAspNet.Config

    Class GetFullConfig

        Public Shared Sub Main(ByVal args() As String)
            Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration("/MyApp")
            config.SaveAs("c:\MyApp.web.config", ConfigurationSaveMode.Full, True)
        End Sub 'Main 

    End Class 

End Namespace
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web.Configuration;

namespace SamplesAspNet.Config
{
    class GetFullConfig
    {
        public static void Main(string[] args)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("/MyApp");
            config.SaveAs("c:\\MyApp.web.config", ConfigurationSaveMode.Full, true);
        }
    }
}

編譯程式碼

請參閱

參考

OpenWebConfiguration

SaveAs

其他資源

HOW TO 主題 - 設定 ASP.NET 應用程式

管理 ASP.NET 網站