栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

AutoUpdater.Net更新Windows服务的方法

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AutoUpdater.Net更新Windows服务的方法

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 = @"SYSTEMCurrentControlSetServices" + 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

当系统检查到有更新,关闭服务,执行更新,替换文件,启动服务,这时服务程序的版本已经更新啦!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/909049.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号