AutoUpdater.Net更新Windows服務的方法
阿新 • • 發佈:2021-01-10
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
當系統檢查到有更新,關閉服務,執行更新,替換檔案,啟動服務,這時服務程式的版本已經更新啦!