window命令大全
1. 创建配置节类
必须创建继承自ConfigurationSection的对象才能进行配置数据读写操作,ConfigurationSection提供了索引器用来获取和设置配置数据,需要注意的是拥有ConfigurationProperty特性的属性才会被存储,并且名称要保持大小写完全一致,如下面的代码中,所有的"id"必须保持一样。
必须创建继承自ConfigurationSection的对象才能进行配置数据读写操作,ConfigurationSection提供了索引器用来获取和设置配置数据,需要注意的是拥有ConfigurationProperty特性的属性才会被存储,并且名称要保持大小写完全一致,如下面的代码中,所有的"id"必须保持一样。
class ConfigSectionData : ConfigurationSection
{
[ConfigurationProperty("id")]
public int Id
{
get { return (int)this["id"]; }
set { this["id"] = value; }
{
[ConfigurationProperty("id")]
public int Id
{
get { return (int)this["id"]; }
set { this["id"] = value; }
痴线 }
[ConfigurationProperty("time")]
public DateTime Time
{
get { return (DateTime)this["time"]; }
set { this["time"] = value; }
}
}
[ConfigurationProperty("time")]
public DateTime Time
{
get { return (DateTime)this["time"]; }
set { this["time"] = value; }
}
}
2. 创建配置文件操作对象
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
吐槽大会曹云金
吐槽大会曹云金
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
config.Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);
data.Id = 1000;
data.Time = DateTime.Now;
config.Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);
上面的例子是操作 fig,在根节点(configuration)下写入名称为"add"的配置数据。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="add" type="ConsoleApplication1.ConfigSectionData, ... />
</configSections>
<add id="1000" time="02/18/2006 21:51:06" />
</configuration>
<configuration>
<configSections>
<section name="add" type="ConsoleApplication1.ConfigSectionData, ... />
</configSections>
<add id="1000" time="02/18/2006 21:51:06" />
</configuration>
需要注意的 VS2005 在IDE模式下会将信息写入 *.fig,并且在程序关闭时覆写该文件,因此您可能看不到您写入的配置数据,只要在资源管理其中执行 *.exe 文件,您就可以在 *.fig 文件中看到结果了。
如果我们需要操作非缺省配置文件,可以使用ExeConfigurationFileMap对象。
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
抗美援朝精神ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
抗美援朝精神ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
config.Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);
公司周年庆典config.Save(ConfigurationSaveMode.Minimal);
如果我们不希望在根节点下写入配置数据,可以使用ConfigurationSectionGroup对象。
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
config.SectionGroups.Add("group1", new ConfigurationSectionGroup());
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
config.SectionGroups.Add("group1", new ConfigurationSectionGroup());
config.SectionGroups["group1"].Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);
config.Save(ConfigurationSaveMode.Minimal);
下面就是生成的配置文件。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="group1" type="System.Configuration.ConfigurationSectionGroup, ... >
<section name="add" type="ConsoleApplication1.ConfigSectionData, ... />
</sectionGroup>
</configSections>
<group1>
<configuration>
<configSections>
<sectionGroup name="group1" type="System.Configuration.ConfigurationSectionGroup, ... >
<section name="add" type="ConsoleApplication1.ConfigSectionData, ... />
</sectionGroup>
</configSections>
<group1>
<add id="1000" time="02/18/2006 22:01:02" />特专业
</group1>
</configuration>
</group1>
</configuration>
3. 读取配置文件
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = config.SectionGroups["group1"].Sections["add"] as ConfigSectionData;
//ConfigSectionData data = config.Sections["add"] as ConfigSectionData; // 从根节读取
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = config.SectionGroups["group1"].Sections["add"] as ConfigSectionData;
//ConfigSectionData data = config.Sections["add"] as ConfigSectionData; // 从根节读取
if (data != null)
{
Console.WriteLine(data.Id);
Console.WriteLine(data.Time);
}
{
Console.WriteLine(data.Id);
Console.WriteLine(data.Time);
}
4. 写配置文件
在写入 ConfigurationSectionGroup 和 ConfigurationSection 前要判断同名配置是否已经存在,否则会写入失败。
另外如果配置文件被其他Configuration对象修改,则保存会失败,并抛出异常。建议采用Singleton模式。
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "fig";
file.ExeConfigFilename = "fig";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 2000;
data.Time = DateTime.Now;
ConfigurationSectionGroup group1 = config.SectionGroups["group1"];
if (group1 == null)
config.SectionGroups.Add("group1", new ConfigurationSectionGroup());
ConfigurationSection data = group1.Sections["add"] as config;
if (add == null)
config.SectionGroups["group1"].Sections.Add("add", data);
else
ConfigSectionData data = new ConfigSectionData();
data.Id = 2000;
data.Time = DateTime.Now;
ConfigurationSectionGroup group1 = config.SectionGroups["group1"];
if (group1 == null)
config.SectionGroups.Add("group1", new ConfigurationSectionGroup());
ConfigurationSection data = group1.Sections["add"] as config;
if (add == null)
config.SectionGroups["group1"].Sections.Add("add", data);
else
{
group1.Sections.Remove("add");
group1.Sections.Add("add", data);类似近身保镖的小说
// 或者直接修改原配置对象,前提是类型转换要成功。
//ConfigSectionData configData = add as ConfigSectionData;
//configData.Id = data.Id;
//configData.Time = data.Time;
}
config.Save(ConfigurationSaveMode.Minimal);
group1.Sections.Remove("add");
group1.Sections.Add("add", data);类似近身保镖的小说
// 或者直接修改原配置对象,前提是类型转换要成功。
//ConfigSectionData configData = add as ConfigSectionData;
//configData.Id = data.Id;
//configData.Time = data.Time;
}
config.Save(ConfigurationSaveMode.Minimal);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论