您好,登錄后才能下訂單哦!
1.問題或需求描述:
程序自我重啟,或在局域網(wǎng)環(huán)境下更新重啟。
2.解決方法或原理:
可創(chuàng)建前臺(tái)或后臺(tái) cmd進(jìn)程,實(shí)現(xiàn)程序更新重啟。
C#代碼:
...
using System.Diagnostics;
using System.IO;
...
private void Restart()
{
//獲得當(dāng)前程序信息
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 請(qǐng)勿中斷,正在重啟程序... & 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)絡(luò)程序位置定義
const string sNetExePath = @"\\192.168.158.25\setup\Test\test.exe";
//獲得當(dāng)前程序信息
Process cur = Process.GetCurrentProcess();
FileInfo fi = new FileInfo(cur.MainModule.FileName);
//獲得網(wǎng)絡(luò)位置程序信息
FileInfo fi2 = new FileInfo(sNetExePath);
//比較文件修改時(shí)間戳,如果網(wǎng)絡(luò)文件較新則更新本地程序并重啟
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 請(qǐng)勿中斷,正在更新程序... & 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
{
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。