C# 远程服务器 安装、卸载 Windows 服务,读取远程注册表,关闭杀掉远程进程

本文介绍了如何使用C#进行远程服务器操作,包括安装和卸载Windows服务,读取远程注册表,以及关闭和杀死远程进程。通过sc命令及ServiceController API实现服务管理,同时强调了服务状态检查和重试机制的重要性,以及在卸载服务时关闭服务窗口以避免阻碍删除操作。此外,提到了所需的System.ServiceProcess和System.Management命名空间。
摘要由CSDN通过智能技术生成

这里安装windows服务我们用sc命令,这里需要远程服务器IP,服务名称、显示名称、描述以及执行文件,安装后需要验证服务是否安装成功,验证方法可以直接调用ServiceController来查询服务,也可以通过远程注册表来查找服务的执行文件;那么卸载文件我们也就用SC命令了,卸载后需要检测是否卸载成功,修改显示名称和描述也用sc命令。至于停止和启动Windows服务我们可以用sc命令也可以用ServiceController的API,当停止失败的时候我们会强制杀掉远程进程,在卸载windows 服务我们需要关闭windows服务窗口,不然无法删除服务(服务被标记为已删除状态)注意安装和卸载Windows服务是通过sc命令来执行的,把sc命令发送到服务器上,服务器需要一段执行时间,所以在远程注册表不一定马上体现安装和卸载的效果,这里我们需要有一个重试的机制远程服务需要引用System.ServiceProcess,关闭远程进程需要引用 System.Management.dll 。先关code如下:

   #region Windows Service 操作
        /// <summary>
        /// 安装Windows 服务
        /// </summary>
        /// <param name="serviceEXEPath">可执行文件的完全路径</param>
        public void Install(string webIp, string serviceName, string displayName, string description, string serviceEXEPath)
        {
            //检查安装文件是否存在
            string remoteFile = PathUtil.GetRemotePath(webIp, serviceEXEPath);
            if (!File.Exists(remoteFile))
            {
                throw new Exception($"安装文件{serviceEXEPath}在服务器{webIp}上不存在");
            }
            //生成安装指令
            string serviceInstallCommand = $"sc \\\\{webIp} create {serviceName} binpath= \"{serviceEXEPath}\" displayname= \"{displayName}\" start= auto ";
            string updateDescriptionCommand = $"sc \\\\{webIp} description {serviceName} \"{description}\" ";
            string[] cmd = new string[] { serviceInstallCommand, updateDescriptionCommand };
            string ss = Cmd(cmd);
            //检查安装是否成功
            string imagePath = GetImagePath(webIp, serviceName);
            int retryTime = 0;
            int maxTime = 3;
            while (retryTime < maxTime && string.IsNullOrEmpty(imagePath))
            {
                Thread.Sleep(1000 * 30);
                retryTime++;
                imagePath = GetImagePath(webIp, serviceName);
            }
            if (string.IsNullOrEmpty(imagePath))
            {
                throw new Exception($"{serviceName}在{webIp}安装失败,{ss}");
            }
        }

        /// <summary>
        /// 卸载Windows 服务
        /// </summary>
        /// <param name="serviceEXEPath">可执行文件的完全路径</param>
        public void Uninstall(string webIp, string serviceName, string serviceEXEPath)
        {
            //关闭mmc.exe进程,如果windows 服务打开 将无法卸载
            var ps = Process.GetProcessesByName(@"mmc", webIp);
            if (ps.Length > 0)
            {
                CloseProcess(webIp, "mmc.exe", string.Empty);
            }

            //检查卸载文件是否存在
            string remoteFile = PathUtil.GetRemotePath(webIp, serviceEXEPath);
            if (!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值