长亭酒一瓢daizhe.blog.chinaunix.net
daizhe
全部博文(282)
Thinking in Java(4)
2013年(1)
2012年(18)
2011年(14)
2010年(30)
2009年(48)
2008年(55)
2007年(55)
2006年(61)
摄影家烈
李元诚
Yoyo_Lou
heu
cynthia
格伯纳
Bsolar
浪花小雨
happyc84
blackcol
simonzh
分类:
2010-01-15 17:12:11
public sealed class ConfigurationSections : ConfigurationSection { [ConfigurationProperty("filename", DefaultValue = "default.txt")] public string FileName { get { return (string)this["filename"]; } set { this["filename"] = value; } } } public sealed class BusinessSpaceConfiguration : ConfigurationSection { [ConfigurationProperty("filename")] public string FileName { get { return (string)this["filename"]; } set { this["filename"] = value; } } }
private static void WriteAppConfiguration() { try { ConfigurationSections configData = new ConfigurationSections(); configData.FileName = "abc.txt"; System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.Sections.Remove("ConfigurationSections"); config.Sections.Add("ConfigurationSections", configData); config.Save(); BusinessSpaceConfiguration bsconfigData = new BusinessSpaceConfiguration(); bsconfigData.FileName = "def.txt"; System.Configuration.Configuration config1 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config1.Sections.Remove("BusinessSpaceConfiguration"); config1.Sections.Add("BusinessSpaceConfiguration", bsconfigData); config1.Save(); } catch (Exception err) { Console.Write(err.Message); } }
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="BusinessSpaceConfiguration" type="ConsoleApplication1.BusinessSpaceConfiguration, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> <section name="ConfigurationSections" type="ConsoleApplication1.ConfigurationSections, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> </configSections> <BusinessSpaceConfiguration filename="def.txt" /> <ConfigurationSections filename="abc.txt" /> </configuration>
private static void ReadAppConfiguration() { ConfigurationSections obj1 = ConfigurationManager.GetSection("ConfigurationSections") as ConfigurationSections; BusinessSpaceConfiguration obj2 = ConfigurationManager.GetSection("BusinessSpaceConfiguration") as BusinessSpaceConfiguration; Console.WriteLine(obj1.FileName); Console.WriteLine(obj2.FileName); }
上一篇:C#读写app.config中的数据
下一篇:自定义应用程序配置文件(app.config)
登录 注册