ConfigurationSectionCollection.Add(String, ConfigurationSection) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
向 ConfigurationSectionCollection 对象添加一个 ConfigurationSection 对象。
public:
void Add(System::String ^ name, System::Configuration::ConfigurationSection ^ section);
public void Add (string name, System.Configuration.ConfigurationSection section);
member this.Add : string * System.Configuration.ConfigurationSection -> unit
Public Sub Add (name As String, section As ConfigurationSection)
参数
- name
- String
要添加的节名称。
- section
- ConfigurationSection
要添加的节。
示例
下面的示例显示如何使用 Add 方法。
static void AddSection()
{
try
{
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
CustomSection customSection =
new CustomSection();
string index =
config.Sections.Count.ToString();
customSection.FileName =
"newFile" + index + ".txt";
string sectionName = "CustomSection" + index;
TimeSpan ts = new TimeSpan(0, 15, 0);
customSection.MaxIdleTime = ts;
customSection.MaxUsers = 100;
config.Sections.Add(sectionName, customSection);
customSection.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}
catch (ConfigurationErrorsException err)
{
Console.WriteLine(err.ToString());
}
}
Shared Sub AddSection()
Try
Dim config _
As System.Configuration.Configuration = _
ConfigurationManager.OpenExeConfiguration( _
ConfigurationUserLevel.None)
Dim customSection _
As New CustomSection()
Dim index As String = _
config.Sections.Count.ToString()
customSection.FileName = _
"newFile" + index + ".txt"
Dim sectionName As String = _
"CustomSection" + index
Dim ts As New TimeSpan(0, 15, 0)
customSection.MaxIdleTime = ts
customSection.MaxUsers = 100
config.Sections.Add(sectionName, customSection)
customSection.SectionInformation.ForceSave = True
config.Save(ConfigurationSaveMode.Full)
Catch err As ConfigurationErrorsException
Console.WriteLine(err.ToString())
End Try
End Sub
注解
默认情况下,如果指定的 ConfigurationSection 对象尚未包含在集合中,则此方法会添加该对象。