C#程序重新启动自己1

 private void ReadConfig()
         {
             Properties.Settings.Default.Reload();
             this.tbFtpIp.Text = Properties.Settings.Default.ftpServer;
             this.tbFtpPort.Text = Properties.Settings.Default.ftpPort;
             this.tbUserName.Text = Properties.Settings.Default.ftpUser;
             this.tbPassword.Text = Properties.Settings.Default.ftpPassword;
             this.tbFtpTimer.Text = Properties.Settings.Default.ftpFrequence;

             this.tbFileType.Text = Properties.Settings.Default.FileType;
             this.tbLocalDirectory.Text = Properties.Settings.Default.localFilePath;
             this.tbTimer.Text = Properties.Settings.Default.Frequence;
         }
         private void btnSaveFtpConfig_Click(object sender, EventArgs e)
         {
             try
             {
                 SaveConfig("ftpServer", tbFtpIp.Text, false);
                 SaveConfig("ftpPort", tbFtpPort.Text, false);
                 SaveConfig("ftpUser", tbUserName.Text, false);
                 SaveConfig("ftpPassword", tbPassword.Text, false);
                 SaveConfig("ftpFrequence", tbFtpTimer.Text, false);
                 SaveConfig("localFilePath", tbLocalDirectory.Text, false);
                 SaveConfig("FileType", tbFileType.Text, false);
                 SaveConfig("Frequence", tbTimer.Text, true);
                 DialogResult dr=MessageBox.Show("配置信息保存成功,请重新启动应用程序","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1);
                 if (dr == DialogResult.Yes)
                 {
                     Application.ExitThread();
                     Restart();
                 }
             }
             catch
             {
                 MessageBox.Show("保存配置信息失败,请确保文件完整");
             }
         }
         private void SaveConfig(string varient, string value, bool Reload)
         {
             // 保存 Applicationi 范围的设置
             string configFileName = Application.ExecutablePath + ".config";
             System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
             doc.Load(configFileName);
             string configString = "configuration/applicationSettings/ftpTransport.Properties.Settings/setting[@name='" + varient + "']/value";
             System.Xml.XmlNode configNode = doc.SelectSingleNode(configString);
             if (configNode != null)
             {
                 configNode.InnerText = value;
                 doc.Save(configFileName);
                 if (Reload)
                 {
                     // 刷新应用程序设置,这样下次读取时才能读到最新的值。
                     Properties.Settings.Default.Reload();
                 }
             }

         }
         private void Restart()
         {
             Thread thtmp = new Thread(new ParameterizedThreadStart(run));
             object appName = Application.ExecutablePath;
             Thread.Sleep(2000);
             thtmp.Start(appName);
         }
         private void run(Object obj)
         {
             Process ps = new Process();
             ps.StartInfo.FileName = obj.ToString();
             ps.Start();
         }

#======================简化一下

C# 重新启动程序(并非重启计算机)
2008年09月16日 星期二 14:33

        private void Restart()
        {
            Thread thtmp = new Thread(new ParameterizedThreadStart(run));
            object appName = Application.ExecutablePath;
            Thread.Sleep(2000);
            thtmp.Start(appName);
        }

        private void run(Object obj)
        {
            Process ps = new Process();
            ps.StartInfo.FileName = obj.ToString();
            ps.Start();
        }

        /// <summary>
        /// 设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfig_Click(object sender, EventArgs e)
        {
            frmHikClientConfig frm = new frmHikClientConfig();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Application.ExitThread();
                Restart();
            }
        }

//============或者(下面的没测试)

clossing事件中,用Process.Start启动一下自己就可以。  
   
   
  在主窗体的closing事件中,加入如下代码:  
   
  if   (   MessageBox.Show("要重新启动嘛?","提示",   MessageBoxButtons.YesNoCancel,  
  MessageBoxIcon.Question)   ==   DialogResult.Yes)  
   
  System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rains卍Soft

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值