ConfigurationSectionCollection.Add(String, ConfigurationSection) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將 ConfigurationSection 物件新增至 ConfigurationSectionCollection 物件。
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 物件。