ConfigurationSection 构造函数
定义
初始化 ConfigurationSection 类的新实例。Initializes a new instance of the ConfigurationSection class.
protected:
ConfigurationSection();
protected ConfigurationSection ();
Protected Sub New ()
示例
下面的示例演示如何使用 ConfigurationSection 构造函数。The following example shows how to use the ConfigurationSection constructor. 此示例假设您已创建了一个名为的自定义节类 CustomSection 。This example assumes that you have created a custom section class named CustomSection. 有关此类类的示例,请参阅 ConfigurationSection 类概述。For an example of such a class, see the ConfigurationSection class overview.
// Create a custom section.
static void CreateSection()
{
try
{
CustomSection customSection;
// Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Create the section entry
// in the <configSections> and the
// related target section in <configuration>.
if (config.Sections["CustomSection"] == null)
{
customSection = new CustomSection();
config.Sections.Add("CustomSection", customSection);
customSection.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}
}
catch (ConfigurationErrorsException err)
{
Console.WriteLine(err.ToString());
}
}
' Create a custom section.
Shared Sub CreateSection()
Try
Dim customSection As CustomSection
' Get the current configuration file.
Dim config As System.Configuration.Configuration = _
ConfigurationManager.OpenExeConfiguration( _
ConfigurationUserLevel.None)
' Create the section entry
' in the <configSections> and the
' related target section in <configuration>.
If config.Sections("CustomSection") Is Nothing Then
customSection = New CustomSection()
config.Sections.Add("CustomSection", customSection)
customSection.SectionInformation.ForceSave = True
config.Save(ConfigurationSaveMode.Full)
End If
Catch err As ConfigurationErrorsException
Console.WriteLine(err.ToString())
End Try
End Sub
注解
若要使用 ConfigurationSection 构造函数,需要先定义自定义节类型。To use the ConfigurationSection constructor, you need to define a custom section type first. 有关示例,请参阅 ConfigurationSection 类概述。For an example see the ConfigurationSection class overview.