c# Winform 应用程序在执行过程中修改了app.config的内容后,怎样使它即使生效???如果需要重新启动应用程序的话,应用程序怎样自己重新启动自己???????请教各位高手!!!

解决方案 »

  1.   

    WinForm不清楚,对于web.config是立即生效的哈,如果你的程序没有缓存设置的话。
      

  2.   

    WinForm 不行!不会要我提示用户:“麻烦您亲自重新启动一下本程序,谢谢!”
      

  3.   

    WinForm的app.config改了以后,在重新读一次,,,不用重启。
      

  4.   

    koenemy() :
    请问怎么重读???
      

  5.   

    错了,是your.exe.config重改,,,,你改app.config干嘛
      

  6.   

    是your.exe.config!不是app.config
    我表达错误
      

  7.   

    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"); 
    }
    上边是修改,微软没提供,修改config的类,只是自己写一个修改xml的方法下面就是读
    this.Str = System.Configuration.ConfigurationSettings.AppSettings["ServerName"];
      

  8.   

    就是我的程序 myApp 在运行的时候, 一个方法修改了 myApp.config 中 <appSettings> 的某一项,我的另一个方法想要读出那一项被修改后的值。请问要怎样?
      

  9.   

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
    <add key="ServerName" value=""/>
    </appSettings>
    </configuration>
      

  10.   

    测试完了,不用重起
    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"];
    }
    }
    }
      

  11.   

    唉,忘说了,你是不是在IDE里编译的。这样不行
    你在IDE里编译完了,去哪个debug里运行你的骗译后的程序,,,,,不要在IDE里按F5什么的运行
      

  12.   

    你不自己启动自己的EXE,然后再把自己关闭就行了。
      

  13.   

    koenemy() : 你的程序只能修改一次,第2次就不起作用了,不信你运行我改过的你的代码: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;
            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"];
            }
        }
    }
      

  14.   

    我加了个textBox1.Textthis.UpdateConfig(textBox1.Text);
      

  15.   

    如果那不是必须写在固定位置的配置信息,就没必要存在config文件里面了,注册表就是一个比较不错的办法,config是配置文件,是系统管理员对程序进行高级配置的,程序自己写配置文件还会遇到文件权限的问题,何苦呢?
      

  16.   

    做Windows开发,就要遵守Windows的规定,不能无视多用户也不能无视NTFS。片面的追求绿色不过是个噱头罢了。要做到真正绿色,就要把配置文件也给省掉,况且写注册表的不一定就不是绿色软件了。其实对于用户而言,好用就成,要不那些流氓软件怎么还是那么多人装呢?。