下載app免費(fèi)領(lǐng)取會員
static void Main(string[] args)
{
//讀取當(dāng)前exe的config:App.config
//讀取appSettings節(jié)點(diǎn)里的數(shù)據(jù)
NameValueCollection nameValues = ConfigurationManager.AppSettings;
string ssssss = nameValues["Key"];
//讀取當(dāng)前exe的config:App.config
//使用exe路徑
Configuration configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
AppSettingsSection ass = configuration.AppSettings;
string sss = ass.Settings["Key"].Value;
//讀取configSections里的數(shù)據(jù)
ConfigurationSectionGroup group = configuration.GetSectionGroup("Group");
AppSettingsSection group1 = (AppSettingsSection)group.Sections.Get("Group1");
string str = group1.Settings["Key"].Value;
Console.WriteLine(str);
//改變值
group1.Settings["Key"].Value = "ChangedValue";
// 保存
configuration.Save();
//讀取dll的config文件:TestDll.dll.config
Type tp = typeof(Class1);
Configuration config = ConfigurationManager.OpenExeConfiguration(tp.Assembly.Location);
ConfigurationSectionGroup _group = config.GetSectionGroup("Group");
AppSettingsSection appss = (AppSettingsSection)_group.Sections.Get("SubGroup");
Console.WriteLine(appss.Settings["TestKey"].Value);
Console.ReadLine();
}
App.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<pGroup name="Group">
<p name="Group1" type="System.Configuration.AppSettingsSection"/>
<p name="Group2" type="System.Configuration.AppSettingsSection"/>
</pGroup>
</configSections>
<Group>
<Group1>
<add key ="Key" value="10"/>
</Group1>
<Group2>
<add key ="Key" value="10"/>
</Group2>
</Group>
<appSettings>
<add key="Key" value="Value"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
TestDll.dll.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<pGroup name="Group">
<p name="SubGroup" type="System.Configuration.AppSettingsSection"/>
</pGroup>
</configSections>
<Group>
<SubGroup>
<add key="TestKey" value="TestValue"/>
</SubGroup>
</Group>
</configuration>
本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。
下一篇:二次開發(fā)教程:Revit開發(fā)自定義警告
推薦專題