1.问题或需求描述:
程序自我重启,或在局域网环境下更新重启。
2.解决方法或原理:
可创建前台或后台 cmd进程,实现程序更新重启。
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
{
//网络程序位置定义
const string sNetExePath = @"\\192.168.158.25\setup\Test\test.exe";
//获得当前程序信息
Process cur = Process.GetCurrentProcess();
FileInfo fi = new FileInfo(cur.MainModule.FileName);
//获得网络位置程序信息
FileInfo fi2 = new FileInfo(sNetExePath);
//比较文件修改时间戳,如果网络文件较新则更新本地程序并重启
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
{
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。