c# appconfig的动态读写问题我建立了一个appconfig 文件。 有一个窗体每次加载时读取appconfig中特定的键值。 还可以修改该键值。
可是修改后 我再加载这个窗体(默认该窗体会再次读取该键值并写在一个控件上) 就无法正确读取修改后的该键值。读到的是
代码如下: namespace checkApp
{
public partial class setupForm : Form
{
string strFileName = @"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\checkApp\checkApp\App.config";
XmlDocument doc = new XmlDocument(); public setupForm()
{
InitializeComponent();
} private void setupForm_Load(object sender, EventArgs e)
{ ConfigurationManager.OpenExeConfiguration(strFileName);
System.Console.WriteLine("!!!!{0}", ConfigurationManager.AppSettings["begin"]);
DateTime be =new DateTime();
be = DateTime.Parse(DateTime.Now.ToShortDateString() + ConfigurationManager.AppSettings["begin"]);
dateTimePicker1.Text = be.ToString();
DateTime end = new DateTime();
end = DateTime.Parse(DateTime.Now.ToShortDateString() + ConfigurationManager.AppSettings["end"]);
dateTimePicker2.Text = end.ToString();
ConfigurationManager.RefreshSection("add"); } private void button1_Click(object sender, EventArgs e)
{ doc.Load(strFileName);
XmlNodeList nodes = doc.GetElementsByTagName("add");
foreach (XmlNode node in nodes)
{
XmlElement xe = (XmlElement)node;
if (xe.GetAttribute("key") == "begin")
xe.SetAttribute("value", " "+dateTimePicker1.Text.ToString());
if (xe.GetAttribute("key") == "end")
xe.SetAttribute("value", " "+dateTimePicker2.Text.ToString()); } ConfigurationManager.RefreshSection("add");
doc.Save(strFileName);
this.Close(); } private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
可是修改后 我再加载这个窗体(默认该窗体会再次读取该键值并写在一个控件上) 就无法正确读取修改后的该键值。读到的是
代码如下: namespace checkApp
{
public partial class setupForm : Form
{
string strFileName = @"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\checkApp\checkApp\App.config";
XmlDocument doc = new XmlDocument(); public setupForm()
{
InitializeComponent();
} private void setupForm_Load(object sender, EventArgs e)
{ ConfigurationManager.OpenExeConfiguration(strFileName);
System.Console.WriteLine("!!!!{0}", ConfigurationManager.AppSettings["begin"]);
DateTime be =new DateTime();
be = DateTime.Parse(DateTime.Now.ToShortDateString() + ConfigurationManager.AppSettings["begin"]);
dateTimePicker1.Text = be.ToString();
DateTime end = new DateTime();
end = DateTime.Parse(DateTime.Now.ToShortDateString() + ConfigurationManager.AppSettings["end"]);
dateTimePicker2.Text = end.ToString();
ConfigurationManager.RefreshSection("add"); } private void button1_Click(object sender, EventArgs e)
{ doc.Load(strFileName);
XmlNodeList nodes = doc.GetElementsByTagName("add");
foreach (XmlNode node in nodes)
{
XmlElement xe = (XmlElement)node;
if (xe.GetAttribute("key") == "begin")
xe.SetAttribute("value", " "+dateTimePicker1.Text.ToString());
if (xe.GetAttribute("key") == "end")
xe.SetAttribute("value", " "+dateTimePicker2.Text.ToString()); } ConfigurationManager.RefreshSection("add");
doc.Save(strFileName);
this.Close(); } private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货