test.config-configuration
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
< configSections >
< section name ="add" type ="WindowsApplication2.ConfigSectionData, WindowsApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</ configSections >
< add id ="1000" time ="03/03/2008 12:55:46" />
</ configuration >
< configuration >
< configSections >
< section name ="add" type ="WindowsApplication2.ConfigSectionData, WindowsApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</ configSections >
< add id ="1000" time ="03/03/2008 12:55:46" />
</ configuration >
再定一个读写类文件
using
System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
namespace WindowsApplication2
... {
class ConfigSectionData : ConfigurationSection
...{
[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; }
}
}
}
using System.Configuration;
using System.Collections.Generic;
using System.Text;
namespace WindowsApplication2
... {
class ConfigSectionData : ConfigurationSection
...{
[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; }
}
}
}
调用(存)
ExeConfigurationFileMap file
=
new
ExeConfigurationFileMap();
file.ExeConfigFilename = " test.config " ;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
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);
file.ExeConfigFilename = " test.config " ;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
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);
取
ExeConfigurationFileMap file
=
new
ExeConfigurationFileMap();
file.ExeConfigFilename = " test.config " ;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data = config.Sections[ " add " ] as ConfigSectionData;
if (data != null )
... {
label1.Text += "data.Id:"+ data.Id; //取ID
label1.Text = "ata.Time;" + data.Time;//取Time
}
ConfigSectionData cs = (ConfigSectionData)config.Sections[ " add " ];
MessageBox.Show(cs.Id.ToString()); // 取ID
file.ExeConfigFilename = " test.config " ;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data = config.Sections[ " add " ] as ConfigSectionData;
if (data != null )
... {
label1.Text += "data.Id:"+ data.Id; //取ID
label1.Text = "ata.Time;" + data.Time;//取Time
}
ConfigSectionData cs = (ConfigSectionData)config.Sections[ " add " ];
MessageBox.Show(cs.Id.ToString()); // 取ID
修改
data.Id
=
12345678
;//修改
config.Save(ConfigurationSaveMode.Modified);//保存
config.Save(ConfigurationSaveMode.Modified);//保存
Full
|
将所有属性都写到配置文件。在创建信息配置文件时或将配置值从一台计算机移动到另一台计算机时最有用
|
Minimal
|
仅将不同于继承值的属性写出到配置文件。
|
Modified
|
仅将修改的属性写出到配置文件,即使值和继承值相同。
|