1.首先是Web.Config配置文件详解的参数含义(具体内容都在以下代码中有注释-我从网上找到的)
1 public class ConfigData : ConfigurationSection 2 { 3 [ConfigurationProperty("ID")] 4 public string ID 5 { 6 get { return (string)this["ID"];} 7 set {this["ID"]=value;} 8 } 9 [ConfigurationProperty("Value")] 10 public string Value 11 { 12 get { return (string)this["Value"]; } 13 set { this["Value"] = value; } 14 } 15 }
<!--appSettings是应用程序设置,可以定义应用程序的全局常量设置等信息-->
<appSettings>
<add key="DataSource" value="(local)" />
<add key="InitialCatalog" value="TEST" />
<add key="UserID" value="sa" />
<add key="Password" value="sa" />
</appSettings>
<!--连接字符串设置-->
<connectionStrings>
<add name="ConnString" connectionString="Data Source=GAO;Initial Catalog=HBWXDate;Us
er ID=sa;password=sa"></add>
<add name="111" connectionString="11111" />
</connectionStrings>
引用:
string DataSoruce = ConfigurationManager.AppSettings["DataSource"].ToString();
string InitialCatalog = ConfigurationManager.AppSettings["InitialCatalog"].ToString();
string UserID = ConfigurationManager.AppSettings["UserID"].ToString();
string Password = ConfigurationManager.AppSettings["Password"].ToString();
2.配置数据读写操作
2.1 继承自ConfigurationSection的子类
只有继承自ConfigurationSection的对象才能进行配置数据读写操作,ConfigurationSection提供了索引器用来获取和设置配置数据,需要注意的是拥有ConfigurationProperty特性的属性才会被存储,并且名称要保持大小写完全一致。
1 public class Config : ConfigurationSection 2 { 3 [ConfigurationProperty("DataSource")] 4 public string DataSource 5 { 6 get {return (string)this["DataSource"];} 7 set { this["DataSource"] = value; } 8 } 9 [ConfigurationProperty("InitialCatalog")] 10 public string DataSource 11 { 12 get { return (string)this["InitialCatalog"]; } 13 set { this["InitialCatalog"] = value; } 14 } 15 [ConfigurationProperty("UserID")] 16 public string DataSource 17 { 18 get { return (string)this["UserID"]; } 19 set { this["UserID"] = value; } 20 } 21 [ConfigurationProperty("Password")] 22 public string DataSource 23 { 24 get { return (string)this["Password"]; } 25 set { this["Password"] = value; } 26 } 27 }
实例化Config类后,就可以通过该类的属性对配置文件进行读写操作了。
2.2 创建配置文件操作对象
1 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 2 Config data = new Config();//... 3 config.Sections.Add("add", data);//在根节点(configuration)下写入名称为"add"的配置数据。 4 config.Save(ConfigurationSaveMode.Minimal);
如果我们需要操作非缺省配置文件,可以使用ExeConfigurationFileMap对象。(缺省配置文件:指系统默认的配置文件)
添加
1 ExeConfigurationFileMap file = new ExeConfigurationFileMap(); 2 file.ExeConfigFilename = "test.config"; 3 Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None); 4 Config data = new Config(); 5 data.DataSource = "(local)"; 6 data.InitiakCatalog= "DB"; 7 conf.Sections.Add("sql", data); 8 conf.Save(ConfigurationSaveMode.Minimal); 9 //如果我们不希望在根节点下写入配置数据,可以使用ConfigurationSectionGroup对象。 10 11 ExeConfigurationFileMap file = new ExeConfigurationFileMap(); 12 file.ExeConfigFilename = "test.config"; 13 Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None); 14 Config data = new Config(); 15 data.DataSource= "1000"; 16 data.UserID = "sa"; 17 conf.SectionGroups.Add("group1", new ConfigurationSectionGroup()); 18 conf.SectionGroups["group1"].Sections.Add("add", data); 19 conf.Save(ConfigurationSaveMode.Minimal);
读取
1 ExeConfigurationFileMap file = new ExeConfigurationFileMap(); 2 file.ExeConfigFilename = "test.config"; 3 Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None); 4 5 Config data = conf.SectionGroups["group1"].Sections["add"] as Config; 6 //Config data = conf.Sections["add"] as Config; // 从根节读取 7 8 if (data != null) 9 { 10 //...
11 }
写入
(在写入 ConfigurationSectionGroup 和 ConfigurationSection 前要判断同名配置是否已经存在,否则会写入失败。
另外如果配置文件被其他Configuration对象修改,则保存会失败,并抛出异常。建议采用Singleton模式。)
1 ExeConfigurationFileMap file = new ExeConfigurationFileMap(); 2 file.ExeConfigFilename = "test.config"; 3 Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None); 4 5 ConfigSectionData data = new ConfigSectionData(); 6 data.Id = 2000; 7 data.Time = DateTime.Now; 8 9 ConfigurationSectionGroup group1 = config.SectionGroups["group1"]; 10 if (group1 == null) 11 config.SectionGroups.Add("group1", new ConfigurationSectionGroup()); 12 13 ConfigurationSection data = group1.Sections["add"] as config; 14 if (add == null) 15 config.SectionGroups["group1"].Sections.Add("add", data); 16 else 17 { 18 group1.Sections.Remove("add"); 19 group1.Sections.Add("add", data); 20 21 // 或者直接修改原配置对象,前提是类型转换要成功。 22 //ConfigSectionData configData = add as ConfigSectionData; 23 //configData.Id = data.Id; 24 //configData.Time = data.Time; 25 } 26 27 config.Save(ConfigurationSaveMode.Minimal);
删除
1 config.SectionGroups.Remove("group1"); 2 //config.SectionGroups.Clear(); 3 4 config.Save(ConfigurationSaveMode.Minimal); 5 6 删除ConfigurationSection 7 config.Sections.Remove("add1"); 8 //config.Sections.Clear(); 9 10 if (config.SectionGroups["group1"] != null) 11 { 12 config.SectionGroups["group1"].Sections.Remove("add2"); 13 //config.SectionGroups["group1"].Sections.Clear(); 14 } 15 16 config.Save(ConfigurationSaveMode.Minimal);
其他
可以使用 ConfigurationManager.OpenMachineConfiguration() 来操作 Machine.config 文件。或使用 System.Web.Configuration 名字空间中的 WebConfigurationManager 类来操作 ASP.net 配置文件。ConfigurationManager还提供了AppSettings、ConnectionStrings、GetSection()等便捷操作。
。。。。。。。。