我的项目中有一个app.config,我想添加一些项目,但是我不想使用“ appSettings”部分。我想在vb.net代码的“项目”部分中动态创建,以验证它是否存在并知道它是否具有此顺序的某些键:
Dim config As Configuration =
ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath)
Dim items As CustomConfigSection =
(CustomConfigSection)configFile.GetSection("Items")
Dim keyItems = ConfigurationManager.AppSettings("Items")
Dim i = 0
for each Section in config.GetSection()
if Not config.GetSection("Items") then
config.Section.Add("Items")
end if
if Not config.AppSettings(Items).Settings.count = 0 then
config.AppSettings(Items).Settings.Add("Items", myVar)
else
for each key in keyItems
i += 1
config.AppSettings.Settings.Add("Items" + i, myVar)
config.Save(ConfigurationSaveMode.Modified)
ConfigurationManager.RefreshSection("Items")
next
end if
else
看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<Items>
<add key="Items" value="val1"></add>
<add key="Items1" value="val2"></add>
<add key="Items2" value="val3"></add>
</Items>
<configuration>
但是我不知道如何操作app.config函数,我真的不想手动添加我的部分。