C# 无法以编程方式安装Windows服务

C# 无法以编程方式安装Windows服务,c#,windows,windows-services,installation,C#,Windows,Windows Services,Installation,几个小时以来,我一直试图通过C#安装Windows服务 当我运行InstallService()函数时,IsInstalled()即使在运行InstallService()之后也返回false,因此我无法启动windows服务 例如: InstallService(); IsInstalled(); // false ServiceBase[] ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(Servic

几个小时以来,我一直试图通过C#安装Windows服务

当我运行
InstallService()
函数时,
IsInstalled()
即使在运行
InstallService()
之后也返回false,因此我无法启动windows服务

例如:

InstallService();
IsInstalled(); // false
ServiceBase[] ServicesToRun = new ServiceBase[] { new Service1() };
ServiceBase.Run(ServicesToRun); //Throws an exception because uninstalled!
这是安装代码,我只显示相关代码:

  private static void InstallService()
    {
        if (IsInstalled()) return;

        try
        {
            using (AssemblyInstaller installer = GetInstaller())
            {
                IDictionary state = new Hashtable();
                try
                {
                    installer.Install(state);
                    installer.Commit(state);
                }
                catch
                {
                    try
                    {
                        installer.Rollback(state);
                    }
                    catch { }
                    throw;
                }
            }
        }
        catch
        {
            throw;
        }
    }


 private static AssemblyInstaller GetInstaller()
        {
            AssemblyInstaller installer = new AssemblyInstaller(
                typeof(Service1).Assembly, null);
            installer.UseNewContext = true;
            return installer;
        }
 private static bool IsInstalled()
        {
            using (ServiceController controller =
                new ServiceController("Service1"))
            {
                try
                {
                    ServiceControllerStatus status = controller.Status;
                }
                catch
                {
                    return false;
                }
                return true;
            }
        }

程序正在运行,对吗?它到底是怎么失败的?谢谢,我刚重新启动了我的电脑。。顺便说一句,你的IsInstalled函数是错误的,bool不能为null。你能检查一下你的服务名称吗;
public static class SelfInstaller
{
    private static readonly string _exePath = Assembly.GetExecutingAssembly().Location;

    public static bool InstallMyService()
    {
        try
        {
            ManagedInstallerClass.InstallHelper(new string[] { _exePath });
        }
        catch
        {
            return false;
        }
        return true;
    }

    public static bool UninstallMyService()
    {
        try
        {
            ManagedInstallerClass.InstallHelper(new string[] { "/u", _exePath });
        }
        catch
        {
            return false;
        }
        return true;
    }
    public static bool IsInstalled(string serviceName)
    {
         var serviceExists = ServiceController.GetServices().Any(s => s.ServiceName == serviceName);
         if (serviceExists == null) return false;
         return true;
    }
}