C# 从exe文件中安装服务

JJYY0088 2016-07-19 11:53:53
已经做好了一个服务,文件名为: TestService.exe ,不如路径位于:D:\TestService.exe

现在想做一个服务的控制器WinForm,可以安装,启动,停止服务,检测到服务不存在时启动安装。

问:如何安装服务 >> D:\TestService.exe (非DOS命令方式) ,安装成功失败有返回值处理
...全文
153 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghui0380 2016-07-19
  • 打赏
  • 举报
回复
当然有,不过你的多搞一步,使用vs自己带的打包程序做个安装包,然后你的代码引用这个安装包,然后这instrall类里面就有Install方法,自己调用这个方法,当然检测也好解决,遍历servcie查找这个名字,如果存在就不安装,不存在就安装
JJYY0088 2016-07-19
  • 打赏
  • 举报
回复
引用 2 楼 starfd 的回复:
不懂 非DOS命令方式…… winform调用cmd,然后传入dos不允许?
就是有没有不调用cmd的方法?
  • 打赏
  • 举报
回复
不懂 非DOS命令方式…… winform调用cmd,然后传入dos不允许?
  • 打赏
  • 举报
回复
什么叫做“非DOS命令方式”?到底对于你的核心功能有什么决定影响?纠结皮毛就可以得到知识?
  • 打赏
  • 举报
回复
Net封装好了,之前写过一个

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.ServiceProcess;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Configuration.Install;

namespace SamosClient.CmdUtility
{
    class ServiceInstall
    {
        /// <summary>
        /// 安装.Net服务,返回是否安装成功
        /// </summary>
        /// <param name="svcPath">服务程序目标路径</param>
        /// <param name="logToConsole">是否将安装日志回显至控制台</param>
        /// <returns>是否安装成功</returns>
        public static bool InstallService(string svcPath, bool logToConsole)
        {
            var title = "Install " + Path.GetFileNameWithoutExtension(svcPath);
            var logtoConsole = "/LogtoConsole=" + logToConsole.ToString().ToLower();
            var assInstall = new AssemblyInstaller(svcPath, new string[] { logtoConsole });
            var installState = new Hashtable();
            try
            {                
                assInstall.Install(installState);
                assInstall.Commit(installState);
                //Trace.Write(Trace.LogType.Installation, title, "Successful.");
                return true;
            }
            catch (Exception exp)
            {
                //Trace.Write(Trace.LogType.Installation, title, exp);
                try
                {
                    assInstall.Rollback(installState);
                }
                catch
                {
                }
                return false;
            }
            finally
            {
                assInstall.Dispose();
            }
        }

        /// <summary>
        /// 卸载.Net服务,返回是否卸载成功
        /// </summary>
        /// <param name="svcPath">服务程序目标路径</param>
        /// <param name="logToConsole">是否将卸载日志回显至控制台</param>
        /// <returns>是否卸载成功</returns>
        public static bool UninstallService(string svcPath, bool logToConsole)
        {
            var logtoConsole = "/LogtoConsole=" + logToConsole.ToString().ToLower();
            var assInstall = new AssemblyInstaller(svcPath, new string[] { logtoConsole });
            var installState = new Hashtable();
            try
            {
                assInstall.Uninstall(installState);
                assInstall.Commit(installState);
                return true;
            }
            catch (Exception exp)
            {
                //Trace.Write(Trace.LogType.Installation, "Uninstall " + Path.GetFileNameWithoutExtension(svcPath), exp);
                return false;
            }
            finally
            {
                assInstall.Dispose();
            }
        }

        #region API 方式安装、卸载

        #region API 函数
        [DllImport("advapi32.dll")]
        internal static extern IntPtr OpenSCManager(string lpMachineName, string lpSCDB, int scParameter);

        [DllImport("advapi32.dll", SetLastError = true)]
        internal static extern IntPtr OpenService(IntPtr SCHANDLE, string lpSvcName, int dwNumServiceArgs);

        [DllImport("advapi32.dll")]
        internal static extern bool DeleteService(IntPtr SVHANDLE);

        [DllImport("advapi32.dll")]
        internal static extern void CloseServiceHandle(IntPtr SCHANDLE);
        #endregion        

        /// <summary>
        /// Win32 API卸载服务
        /// </summary>
        /// <param name="svcName">服务名称</param>
        public static bool Win32_UninstallService(string svcName)
        {
            int GENERIC_WRITE = 0x40000000, DELETE = 0x10000;
            IntPtr sc_hnd = IntPtr.Zero, svc_hnd = IntPtr.Zero;
            try
            {
                sc_hnd = OpenSCManager(null, null, GENERIC_WRITE);
                if (sc_hnd == IntPtr.Zero)
                {
                    //TraceWin32Error("Uninstall " + svcName, "OpenSCManager");
                    return false;
                }
                svc_hnd = OpenService(sc_hnd, svcName, DELETE);
                if (svc_hnd == IntPtr.Zero)
                {
                    //TraceWin32Error("Uninstall " + svcName, "OpenService");
                    return false;
                }
                return DeleteService(svc_hnd);
            }
            finally
            {
                if (svc_hnd != IntPtr.Zero) CloseServiceHandle(svc_hnd);
                if (sc_hnd != IntPtr.Zero) CloseServiceHandle(sc_hnd);
            }
        }
        #endregion        
    }
}
.net方式安装服务实际上将安装信息序列化保存到xxx.InstallState,如果你将这文件删掉,.Net方式就会卸载失败,但API方式可以

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧