简体   繁体   中英

Unable to install a Windows Service programmatically

I've been trying to install a Windows Service via C# for a few hours.

When I run the InstallService() function, IsInstalled() returns false even after the InstallService() run, and thus I'm unable to start the windows service.

For Example:

InstallService();
IsInstalled(); // false
ServiceBase[] ServicesToRun = new ServiceBase[] { new Service1() };
ServiceBase.Run(ServicesToRun); //Throws an exception because uninstalled!

So here's the installation code, I'm showing only the relevant code:

  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;
            }
        }
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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM