1.問題或需求描述:
程序自我重啟,或在局域網(wǎng)環(huán)境下更新重啟。
2.解決方法或原理:
可創(chuàng)建前臺或后臺 cmd進程,實現(xiàn)程序更新重啟。
C#代碼:
...
using System.Diagnostics;
using System.IO;
...
private void Restart()
{
//獲得當前程序信息
Process cur = Process.GetCurrentProcess();
FileInfo fi = new FileInfo(cur.MainModule.FileName);
try
{
Process subPro = new Process();
subPro.StartInfo.UseShellExecute = true;
subPro.StartInfo.FileName = "cmd";
subPro.StartInfo.Arguments = " /q /c echo 請勿中斷,正在重啟程序... & ping -n 3 127.1 1>nul 2>nul & taskkill /pid "
+ cur.Id + " 1>nul 2>nul & start \"\" /max \""
+ cur.MainModule.FileName + "\"";
subPro.StartInfo.CreateNoWindow = false;
subPro.Start();
}
catch
{
//Add your code
}
finally
{
Close();
}
}
private void Update()
{
try
{
//網(wǎng)絡程序位置定義
const string sNetExePath = @"\\192.168.158.25\setup\Test\test.exe";
//獲得當前程序信息
Process cur = Process.GetCurrentProcess();
FileInfo fi = new FileInfo(cur.MainModule.FileName);
//獲得網(wǎng)絡位置程序信息
FileInfo fi2 = new FileInfo(sNetExePath);
//比較文件修改時間戳,如果網(wǎng)絡文件較新則更新本地程序并重啟
if (DateTime.Compare(fi2.LastWriteTime, fi.LastWriteTime) > 0)
{
try
{
Process subPro = new Process();
subPro.StartInfo.UseShellExecute = true;
subPro.StartInfo.FileName = "cmd";
subPro.StartInfo.Arguments = " /q /c echo 請勿中斷,正在更新程序... & ping -n 3 127.1 1>nul 2>nul & taskkill /pid "
+ cur.Id + " 1>nul 2>nul & copy /y \""
+ sNetExePath + "\" \""
+ cur.MainModule.FileName
+ "\" 1>nul 2>nul && start \"\" /max \""
+ cur.MainModule.FileName + "\"";
subPro.StartInfo.CreateNoWindow = false;
subPro.Start();
}
catch
{
}
finally
{
Close();
}
}
}
catch
{
}
}
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡助力業(yè)務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調(diào)度,確保服務器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務器買多久送多久。