İngilizce dilinde oku

Aracılığıyla paylaş


Configuration Sınıf

Tanım

Belirli bir bilgisayar, uygulama veya kaynak için geçerli olan bir yapılandırma dosyasını temsil eder. Bu sınıf devralınamaz.

C#
public sealed class Configuration
Devralma
Configuration

Örnekler

Aşağıdaki kod örneği, yapılandırma dosyası öğelerine erişmek için sınıfının nasıl kullanılacağını Configuration gösterir.

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Specialized;

// Before compiling this application, 
// remember to reference the System.Configuration assembly in your project. 
#region CustomSection class

// Define a custom section. This class is used to
// populate the configuration file.
// It creates the following custom section:
//  <CustomSection name="Contoso" url="http://www.contoso.com" port="8080" />.
public sealed class CustomSection : ConfigurationSection
{

    public CustomSection()
    {
    }

    [ConfigurationProperty("name",
     DefaultValue = "Contoso",
     IsRequired = true,
     IsKey = true)]
    public string Name
    {
        get
        {
            return (string)this["name"];
        }
        set
        {
            this["name"] = value;
        }
    }

    [ConfigurationProperty("url",
        DefaultValue = "http://www.contoso.com",
        IsRequired = true)]
    [RegexStringValidator(@"\w+:\/\/[\w.]+\S*")]
    public string Url
    {
        get
        {
            return (string)this["url"];
        }
        set
        {
            this["url"] = value;
        }
    }

    [ConfigurationProperty("port",
        DefaultValue = (int)8080,
        IsRequired = false)]
    [IntegerValidator(MinValue = 0,
        MaxValue = 8080, ExcludeRange = false)]
    public int Port
    {
        get
        {
            return (int)this["port"];
        }
        set
        {
            this["port"] = value;
        }
    }
}

#endregion

#region Using Configuration Class
class UsingConfigurationClass
{


    // Show how to create an instance of the Configuration class
    // that represents this application configuration file.  
    static void CreateConfigurationFile()
    {
        try
        {

            // Create a custom configuration section.
            CustomSection customSection = new CustomSection();

            // Get the current configuration file.
            System.Configuration.Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None);

            // Create the custom section entry  
            // in <configSections> group and the 
            // related target section in <configuration>.
            if (config.Sections["CustomSection"] == null)
            {
                config.Sections.Add("CustomSection", customSection);
            }

            // Create and add an entry to appSettings section.
            
            string conStringname="LocalSqlServer";
            string conString = @"data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true";
            string providerName="System.Data.SqlClient";

            ConnectionStringSettings connStrSettings = new ConnectionStringSettings();
            connStrSettings.Name = conStringname;
            connStrSettings.ConnectionString= conString;
            connStrSettings.ProviderName = providerName;

            config.ConnectionStrings.ConnectionStrings.Add(connStrSettings);
            
            // Add an entry to appSettings section.
            int appStgCnt =
                ConfigurationManager.AppSettings.Count;
            string newKey = "NewKey" + appStgCnt.ToString();

            string newValue = DateTime.Now.ToLongDateString() +
              " " + DateTime.Now.ToLongTimeString();

            config.AppSettings.Settings.Add(newKey, newValue);

            // Save the configuration file.
            customSection.SectionInformation.ForceSave = true;
            config.Save(ConfigurationSaveMode.Full);

            Console.WriteLine("Created configuration file: {0}",
                config.FilePath);
        }
        catch (ConfigurationErrorsException err)
        {
            Console.WriteLine("CreateConfigurationFile: {0}", err.ToString());
        }
    }

    // Show how to use the GetSection(string) method.
    static void GetCustomSection()
    {
        try
        {

            CustomSection customSection;

            // Get the current configuration file.
            System.Configuration.Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None) as Configuration;

            customSection =
                config.GetSection("CustomSection") as CustomSection;

            Console.WriteLine("Section name: {0}", customSection.Name);
            Console.WriteLine("Url: {0}", customSection.Url);
            Console.WriteLine("Port: {0}", customSection.Port);
        }
        catch (ConfigurationErrorsException err)
        {
            Console.WriteLine("Using GetSection(string): {0}", err.ToString());
        }
    }


    // Show how to use different modalities to save 
    // a configuration file.
    static void SaveConfigurationFile()
    {
        try
        {

            // Get the current configuration file.
            System.Configuration.Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None) as Configuration;

            // Save the full configuration file and force save even if the file was not modified.
            config.SaveAs("MyConfigFull.config", ConfigurationSaveMode.Full, true);
            Console.WriteLine("Saved config file as MyConfigFull.config using the mode: {0}",
                ConfigurationSaveMode.Full.ToString());

            config =
                    ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None) as Configuration;

            // Save only the part of the configuration file that was modified. 
            config.SaveAs("MyConfigModified.config", ConfigurationSaveMode.Modified, true);
            Console.WriteLine("Saved config file as MyConfigModified.config using the mode: {0}",
                ConfigurationSaveMode.Modified.ToString());

            config =
                    ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None) as Configuration;

            // Save the full configuration file.
            config.SaveAs("MyConfigMinimal.config");
            Console.WriteLine("Saved config file as MyConfigMinimal.config using the mode: {0}",
                ConfigurationSaveMode.Minimal.ToString());
        }
        catch (ConfigurationErrorsException err)
        {
            Console.WriteLine("SaveConfigurationFile: {0}", err.ToString());
        }
    }

    // Show how use the AppSettings and ConnectionStrings 
    // properties.
    static void GetSections(string section)
    {
        try
        {

            // Get the current configuration file.
            System.Configuration.Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None) as Configuration;

            // Get the selected section.
            switch (section)
            {
                case "appSettings":
                    try
                    {
                        AppSettingsSection appSettings =
                            config.AppSettings as AppSettingsSection;
                        Console.WriteLine("Section name: {0}",
                                appSettings.SectionInformation.SectionName);

                        // Get the AppSettings section elements.
                        Console.WriteLine();
                        Console.WriteLine("Using AppSettings property.");
                        Console.WriteLine("Application settings:");
                        // Get the KeyValueConfigurationCollection 
                        // from the configuration.
                        KeyValueConfigurationCollection settings =
                          config.AppSettings.Settings;

                        // Display each KeyValueConfigurationElement.
                        foreach (KeyValueConfigurationElement keyValueElement in settings)
                        {
                            Console.WriteLine("Key: {0}", keyValueElement.Key);
                            Console.WriteLine("Value: {0}", keyValueElement.Value);
                            Console.WriteLine();
                        }
                    }
                    catch (ConfigurationErrorsException e)
                    {
                        Console.WriteLine("Using AppSettings property: {0}",
                            e.ToString());
                    }
                    break;

                case "connectionStrings":
                    ConnectionStringsSection
                        conStrSection =
                        config.ConnectionStrings as ConnectionStringsSection;
                    Console.WriteLine("Section name: {0}",
                        conStrSection.SectionInformation.SectionName);

                    try
                    {
                        if (conStrSection.ConnectionStrings.Count != 0)
                        {
                            Console.WriteLine();
                            Console.WriteLine("Using ConnectionStrings property.");
                            Console.WriteLine("Connection strings:");

                            // Get the collection elements.
                            foreach (ConnectionStringSettings connection in
                              conStrSection.ConnectionStrings)
                            {
                                string name = connection.Name;
                                string provider = connection.ProviderName;
                                string connectionString = connection.ConnectionString;

                                Console.WriteLine("Name:               {0}",
                                  name);
                                Console.WriteLine("Connection string:  {0}",
                                  connectionString);
                                Console.WriteLine("Provider:            {0}",
                                   provider);
                            }
                        }
                    }
                    catch (ConfigurationErrorsException e)
                    {
                        Console.WriteLine("Using ConnectionStrings property: {0}",
                            e.ToString());
                    }
                    break;

                default:
                    Console.WriteLine(
                        "GetSections: Unknown section (0)", section);
                    break;
            }
        }
        catch (ConfigurationErrorsException err)
        {
            Console.WriteLine("GetSections: (0)", err.ToString());
        }
    }

    // Show how to use the Configuration object properties 
    // to obtain configuration file information.
    static void GetConfigurationInformation()
    {
        try
        {

            // Get the current configuration file.
            System.Configuration.Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None) as Configuration;

            Console.WriteLine("Reading configuration information:");

            ContextInformation evalContext =
                config.EvaluationContext as ContextInformation;
            Console.WriteLine("Machine level: {0}",
                evalContext.IsMachineLevel.ToString());
                    
            string filePath = config.FilePath;
            Console.WriteLine("File path: {0}", filePath);
             
            bool hasFile = config.HasFile;
            Console.WriteLine("Has file: {0}", hasFile.ToString());

            ConfigurationSectionGroupCollection
                groups = config.SectionGroups;
            Console.WriteLine("Groups: {0}", groups.Count.ToString());
            foreach (ConfigurationSectionGroup group in groups)
            {
                Console.WriteLine("Group Name: {0}", group.Name);
               // Console.WriteLine("Group Type: {0}", group.Type);
            }

            ConfigurationSectionCollection
                sections = config.Sections;
            Console.WriteLine("Sections: {0}", sections.Count.ToString());
        }
        catch (ConfigurationErrorsException err)
        {
            Console.WriteLine("GetConfigurationInformation: {0}",err.ToString());
        }
    }

#endregion 

#region Application Main
    //*** User Interaction Class ***//

    // Obtain user's input and provide feedback.
    // This class contains the application Main() function.
    // It calls the ConfigurationManager methods based 
    // on the user's selection.
    class ApplicationMain
    {
        // Display user's menu.
        public static void UserMenu()
        {
            string applicationName =
                Environment.GetCommandLineArgs()[0] + ".exe";
            StringBuilder buffer = new StringBuilder();

            buffer.AppendLine("Application: " + applicationName);
            buffer.AppendLine("Make your selection.");
            buffer.AppendLine("?    -- Display help.");
            buffer.AppendLine("Q,q  -- Exit the application.");
            
            buffer.Append("1    -- Instantiate the");
            buffer.AppendLine(" Configuration class.");

            buffer.Append("2    -- Use GetSection(string) to read ");
            buffer.AppendLine(" a custom section.");
            
            buffer.Append("3    -- Use SaveAs methods");
            buffer.AppendLine(" to save the configuration file.");

            buffer.Append("4    -- Use AppSettings property to read");
            buffer.AppendLine(" the appSettings section.");
            buffer.Append("5    -- Use ConnectionStrings property to read");
            buffer.AppendLine(" the connectionStrings section.");

            buffer.Append("6    -- Use Configuration class properties");
            buffer.AppendLine(" to obtain configuration information.");

            Console.Write(buffer.ToString());
        }

        // Obtain user's input and provide
        // feedback.
        static void Main(string[] args)
        {
            // Define user selection string.
            string selection;

            // Get the name of the application.
            string appName =
                Environment.GetCommandLineArgs()[0];

            // Get user selection.
            while (true)
            {

                UserMenu();
                Console.Write("> ");
                selection = Console.ReadLine();
                if (!string.IsNullOrEmpty(selection))
                    break;
            }

            while (selection.ToLower() != "q")
            {
                // Process user's input.
                switch (selection)
                {
                    case "1":
                        // Show how to create an instance of the Configuration class.
                        CreateConfigurationFile();
                        break;

                    case "2":
                        // Show how to use GetSection(string) method.
                        GetCustomSection();
                        break;

                    case "3":
                        // Show how to use ConnectionStrings.
                        SaveConfigurationFile();
                        break;

                    case "4":
                        // Show how to use the AppSettings property.
                        GetSections("appSettings");
                        break;

                    case "5":
                        // Show how to use the ConnectionStrings property.
                        GetSections("connectionStrings");
                        break;

                    case "6":
                        // Show how to obtain configuration file information.
                        GetConfigurationInformation();
                        break;

                    default:
                        UserMenu();
                        break;
                }
                Console.Write("> ");
                selection = Console.ReadLine();
            }
        }
    }
#endregion

}

Açıklamalar

Yapılandırma ayarları bir yapılandırma dosyaları hiyerarşisinde depolanır. Configuration Sınıf örneği, bilgisayar gibi belirli bir fiziksel varlığa veya uygulama ya da Web sitesi gibi bir mantıksal varlığa uygulanan tüm yapılandırma dosyalarındaki yapılandırma ayarlarının birleştirilmiş görünümünü temsil eder. Mantıksal varlık yerel bilgisayarda veya uzak sunucuda bulunabilir. Yapılandırma dosyaları hakkında bilgi için bkz. Uygulamaları Yapılandırma ve Yapılandırma Dosyalarını ASP.NET.

Belirtilen varlık için yapılandırma dosyası olmadığında, Configuration nesne Machine.config dosyası tarafından tanımlanan varsayılan yapılandırma ayarlarını temsil eder.

Aşağıdaki sınıfları kullanarak bir Configuration nesne alabilirsiniz:

Nesne Configuration döndüren yöntemlerin adları "Aç" ile başlar.

Bir nesnedeki yapılandırma ayarlarını temsil eden bir Configuration yapılandırma dosyası da oluşturabilirsiniz. Bunu yapmak için aşağıdaki yöntemlerden birini kullanın:

  • Save Yeni bir yapılandırma dosyası oluşturmak için yöntemini çağırın.

  • SaveAs Başka bir konumda yeni bir yapılandırma dosyası oluşturmak için yöntemini çağırın.

Yapılandırma dosyaları oluşturan yöntemlerin adları "Kaydet" ile başlar.

Not

Uzak bilgisayarda yapılandırma ayarlarına erişimi etkinleştirmek için Aspnet_regiis komut satırı aracını kullanın. Bu araç hakkında daha fazla bilgi için bkz. iis kayıt aracı (Aspnet_regiis.exe) ASP.NET. .NET Framework'te yer alan iç bölümler dışındaki özel yapılandırma ayarlarını oluşturma ve bunlara erişme hakkında bilgi için ConfigurationSectionbkz. .

Devralanlara Notlar

sınıfı, Configuration yapılandırma dosyalarını düzenlemek için programlı erişim sağlar. Web uygulamaları için sınıfı veya istemci uygulamaları için sınıfı tarafından WebConfigurationManagerConfigurationManager sağlanan "Aç" yöntemlerinden birini kullanırsınız. Bu yöntemler, temel yapılandırma dosyalarını işleyen yöntemleri ve özellikleri sağlayan bir Configuration nesnesi döndürür. Yapılandırma bilgilerini okumak veya yazmak için bu dosyalara erişebilirsiniz.

Yapılandırma bilgilerini okumak için yöntemini veya GetSectionGroup(String) yöntemini kullanırsınızGetSection(String). Okuyan kullanıcının veya işlemin aşağıdaki izinlere sahip olması gerektiğini unutmayın:

  • Geçerli yapılandırma hiyerarşisi düzeyinde yapılandırma dosyasında okuma izni.

  • Tüm üst yapılandırma dosyalarındaki okuma izinleri.

Uygulamanızın kendi yapılandırmasına salt okunur erişime ihtiyacı varsa, Web uygulamaları için yöntem aşırı yüklemelerini kullanmanız GetSection önerilir. İstemci uygulaması için yöntemini kullanın GetSection(String) .

Bu yöntemler, geçerli uygulama için önbelleğe alınmış yapılandırma değerlerine erişim sağlar ve bu da sınıftan daha iyi performansa Configuration sahiptir.

Not: Yol parametresini alan statik GetSection bir yöntem kullanırsanız, yol parametresi kodun çalıştığı uygulamaya başvurmalıdır, aksi takdirde parametre yoksayılır ve çalışmakta olan uygulamanın yapılandırma bilgileri döndürülür.

Yapılandırma bilgilerini yazmak için yöntemlerden birini Save kullanırsınız. Yazan kullanıcının veya işlemin aşağıdaki izinlere sahip olması gerektiğini unutmayın:

  • Geçerli yapılandırma hiyerarşisi düzeyinde yapılandırma dosyası ve dizini üzerinde yazma izni.

  • Tüm yapılandırma dosyalarındaki okuma izinleri.

Özellikler

AppSettings

Bu Configuration nesneye AppSettingsSection uygulanan nesne yapılandırma bölümünü alır.

AssemblyStringTransformer

Yapılandırma dosyalarındaki derleme dizelerini dönüştürmek için kullanılan işlev temsilcisini belirtir.

ConnectionStrings

Bu Configuration nesneye uygulanan bir ConnectionStringsSection yapılandırma bölümü nesnesi alır.

EvaluationContext

Nesnenin ContextInformation nesnesini Configuration alır.

FilePath

Bu Configuration nesne tarafından temsil edilen yapılandırma dosyasının fiziksel yolunu alır.

HasFile

Bu Configuration nesneyle temsil edilen kaynak için bir dosyanın var olup olmadığını gösteren bir değer alır.

Locations

Bu Configuration nesne içinde tanımlanan konumları alır.

NamespaceDeclared

Yapılandırma dosyasının XML ad alanına sahip olup olmadığını belirten bir değer alır veya ayarlar.

RootSectionGroup

Bu Configuration nesnenin kökünü ConfigurationSectionGroup alır.

SectionGroups

Bu yapılandırma tarafından tanımlanan bölüm gruplarının koleksiyonunu alır.

Sections

Bu Configuration nesne tarafından tanımlanan bölümlerin koleksiyonunu alır.

TargetFramework

Geçerli sürümden önceki bir sürüm hedeflendiğinde hedeflenen .NET sürümünü alır veya ayarlar.

TypeStringTransformer

Yapılandırma dosyalarındaki tür dizelerini dönüştürmek için kullanılan işlev temsilcisini belirtir.

Yöntemler

Equals(Object)

Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler.

(Devralındığı yer: Object)
GetHashCode()

Varsayılan karma işlevi işlevi görür.

(Devralındığı yer: Object)
GetSection(String)

Belirtilen ConfigurationSection nesneyi döndürür.

GetSectionGroup(String)

Belirtilen ConfigurationSectionGroup nesneyi alır.

GetType()

Type Geçerli örneğini alır.

(Devralındığı yer: Object)
MemberwiseClone()

Geçerli Objectöğesinin sığ bir kopyasını oluşturur.

(Devralındığı yer: Object)
Save()

Bu Configuration nesnenin içinde yer alan yapılandırma ayarlarını geçerli XML yapılandırma dosyasına yazar.

Save(ConfigurationSaveMode, Boolean)

Bu Configuration nesnenin içinde yer alan yapılandırma ayarlarını geçerli XML yapılandırma dosyasına yazar.

Save(ConfigurationSaveMode)

Bu Configuration nesnenin içinde yer alan yapılandırma ayarlarını geçerli XML yapılandırma dosyasına yazar.

SaveAs(String, ConfigurationSaveMode, Boolean)

Bu Configuration nesnenin içinde yer alan yapılandırma ayarlarını belirtilen XML yapılandırma dosyasına yazar.

SaveAs(String, ConfigurationSaveMode)

Bu Configuration nesnenin içinde yer alan yapılandırma ayarlarını belirtilen XML yapılandırma dosyasına yazar.

SaveAs(String)

Bu Configuration nesnenin içinde yer alan yapılandırma ayarlarını belirtilen XML yapılandırma dosyasına yazar.

ToString()

Geçerli nesneyi temsil eden dizeyi döndürür.

(Devralındığı yer: Object)

Şunlara uygulanır

Ürün Sürümler
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Ayrıca bkz.