c# Winform 应用程序在执行过程中修改了app.config的内容后,怎样使它即使生效???如果需要重新启动应用程序的话,应用程序怎样自己重新启动自己???????请教各位高手!!!
解决方案 »
- 求一个WPF统计图(饼、柱、曲线)demo或案例,不求3D炫,只求代码简
- sql问题
- 重命名就要重构!!急!,有的页面不需重构,新添的控件在后台就找不到
- 基于VS2005构建的静态搜索引擎和AJAX树,等其他源码发布共享
- vs2005中,调动API钩子函数过程中,我用WH_KEYBOARD_LL = 13钩键盘,击键时回调函数没工作,还有大小如何区分啊?
- 1000分求一大型应用程序的权限设计框架源码。
- 在DataGridz中,离开前的事件!
- 服务器css部分不显
- 自定义控件在开发过程中看不到效果怎么回事,请高手帮忙。
- 关于执行 SqlCommand com = new SqlCommand(sql,con);的问题
- 找个漂亮得滚动条
- 谁知道Visual Studio 2005 Team System suite 多少M?
请问怎么重读???
我表达错误
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.ExecutablePath+".config");
XmlNode node = doc.SelectSingleNode(@"//add[@key='ServerName']");
XmlElement ele = (XmlElement)node;
ele.SetAttribute("value",Xvalue);
doc.Save(Application.ExecutablePath+".config");
}
上边是修改,微软没提供,修改config的类,只是自己写一个修改xml的方法下面就是读
this.Str = System.Configuration.ConfigurationSettings.AppSettings["ServerName"];
<configuration>
<appSettings>
<add key="ServerName" value=""/>
</appSettings>
</configuration>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
namespace TestUpdateConfig
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 96);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(120, 160);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void UpdateConfig(string Xvalue)
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.ExecutablePath+".config");
XmlNode node = doc.SelectSingleNode(@"//add[@key='ServerName']");
XmlElement ele = (XmlElement)node;
ele.SetAttribute("value",Xvalue);
doc.Save(Application.ExecutablePath+".config");
} private void button1_Click(object sender, System.EventArgs e)
{
this.UpdateConfig("yxz");
} private void button2_Click(object sender, System.EventArgs e)
{
this.Text = System.Configuration.ConfigurationSettings.AppSettings["ServerName"];
}
}
}
你在IDE里编译完了,去哪个debug里运行你的骗译后的程序,,,,,不要在IDE里按F5什么的运行
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
namespace TestUpdateConfig
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 96);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(120, 160);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(48, 56);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 2;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void UpdateConfig(string Xvalue)
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.ExecutablePath+".config");
XmlNode node = doc.SelectSingleNode(@"//add[@key='ServerName']");
XmlElement ele = (XmlElement)node;
ele.SetAttribute("value",Xvalue);
doc.Save(Application.ExecutablePath+".config");
} private void button1_Click(object sender, System.EventArgs e)
{
this.UpdateConfig(textBox1.Text);
} private void button2_Click(object sender, System.EventArgs e)
{
this.Text = System.Configuration.ConfigurationSettings.AppSettings["ServerName"];
}
}
}