AutoUpdater服务程序无法更新,但是劳动人民的智慧是无穷的,通过一个独立的.Net程序来监测更新,有更新,关闭服务,进行更新,然后在运行服务
AutoUpdater不太会用请看下面链接
https://blog.csdn.net/g313105910/article/details/112313695
新建一个.net控制台程序
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using AutoUpdaterDotNET;
namespace ServerUpData
{
class Program
{
static string serverName = "你要更新的服务名称";
static void Main(string[] args)
{
AutoUpdater.ApplicationExitEvent += AutoUpdater_ApplicationExitEvent;
string serverPath = GetExecutablePathForService(serverName);
Console.WriteLine(serverPath);
string version = FileVersionInfo.GetVersionInfo(serverPath).FileVersion;
Console.WriteLine(version);
//指定程序版本
AutoUpdater.InstalledVersion = new Version(version);
ServiceRunning(serverName);
Console.WriteLine("启动服务!");
Thread.Sleep(10000);
while (true)
{
if (!ServiceIsRunning(serverName))
{
Console.WriteLine("服务未启动!");
Thread.Sleep(5000);
}
else
{
Console.WriteLine("服务已启动!检查更新");
AutoUpdater.Start("http://10.0.0.7:7777/updates/ServerUpDataAutoUpdater.xml");
break;
}
}
Console.WriteLine("准备退出!");
Console.ReadKey();
}
private static void AutoUpdater_ApplicationExitEvent()
{
Console.WriteLine("关闭服务!");
ServiceStoping(serverName);
while (true)
{
if (!ServiceIsRunning(serverName))
{
Console.WriteLine("服务已关闭!");
Thread.Sleep(5000);
Process.GetCurrentProcess().Kill();
}
else
{
Console.WriteLine("服务未关闭!");
}
}
}
private static string GetExecutablePathForService(string serviceName, RegistryView registryView = RegistryView.Default, bool throwErrorIfNonExisting = false)
{
string registryPath = @"SYSTEM\CurrentControlSet\Services\" + serviceName;
RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView).OpenSubKey(registryPath);
if (key == null)
{
if (throwErrorIfNonExisting)
throw new ArgumentException("Non-existent service: " + serviceName, "serviceName");
else
return null;
}
string value = key.GetValue("ImagePath").ToString();
key.Close();
if (value.StartsWith("\""))
{
value = Regex.Match(value, "\"([^\"]+)\"").Groups[1].Value;
}
return Environment.ExpandEnvironmentVariables(value);
}
public static bool ServiceIsRunning(string serviceName)
{
if (serviceName != null)
{
bool result = false;
System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();
foreach (System.ServiceProcess.ServiceController sc in services)
{
if (sc.ServiceName.ToLower().Contains(serviceName.ToLower()))
{
if (sc.Status.ToString().ToLower() == "running")
{
result = true;
break;
}
}
}
return result;
}
return false;
}
public static bool ServiceRunning(string serviceName)
{
if (serviceName != null)
{
bool result = false;
System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();
foreach (System.ServiceProcess.ServiceController sc in services)
{
if (sc.ServiceName.ToLower().Contains(serviceName.ToLower()))
{
if (sc.Status.ToString().ToLower() == "running")
{
result = true;
break;
}
else
{
sc.Start();
sc.Refresh();
}
}
}
return result;
}
return false;
}
public static bool ServiceStoping(string serviceName)
{
if (serviceName != null)
{
bool result = false;
System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();
foreach (System.ServiceProcess.ServiceController sc in services)
{
if (sc.ServiceName.ToLower().Contains(serviceName.ToLower()))
{
if (sc.Status.ToString().ToLower() == "running")
{
sc.Stop();
sc.Refresh();
result = true;
break;
}
}
}
return result;
}
return false;
}
}
}
编译生成,将生成的文件复制到服务程序目录
在系统中建一个计划任务,可以是1小时启动一次,也可以5分钟,将任务程序指向到ServerUpData.exe
当系统检查到有更新,关闭服务,执行更新,替换文件,启动服务,这时服务程序的版本已经更新啦!