public static void InstallService(string serviceName, string displayName, string fileName)
    {
        IntPtr intPtr = ServiceInstaller.OpenSCManager(ScmAccessRights.AllAccess);

        try
        {
            IntPtr value = ServiceInstaller.OpenService(intPtr, serviceName, ServiceAccessRights.AllAccess);
            if (value == IntPtr.Zero)
            {
                value = ServiceInstaller.CreateService(intPtr, serviceName, displayName, ServiceAccessRights.AllAccess, 16, ServiceBootFlag.AutoStart, ServiceError.Normal, fileName, null, IntPtr.Zero, null, null, null);
            }
            value == IntPtr.Zero;
        }
        catch (Exception)
        {
        }
        finally
        {
            ServiceInstaller.CloseServiceHandle(intPtr);
        }
    }
    public static bool ServiceIsInstalled(string serviceName)
    {
        IntPtr intPtr = ServiceInstaller.OpenSCManager(ScmAccessRights.Connect);
        bool   result;

        try
        {
            IntPtr intPtr2 = ServiceInstaller.OpenService(intPtr, serviceName, ServiceAccessRights.QueryStatus);
            if (intPtr2 == IntPtr.Zero)
            {
                result = false;
            }
            else
            {
                ServiceInstaller.CloseServiceHandle(intPtr2);
                result = true;
            }
        }
        finally
        {
            ServiceInstaller.CloseServiceHandle(intPtr);
        }
        return(result);
    }
    public static void StartService(string serviceName)
    {
        IntPtr intPtr = ServiceInstaller.OpenSCManager(ScmAccessRights.Connect);

        try
        {
            IntPtr intPtr2 = ServiceInstaller.OpenService(intPtr, serviceName, ServiceAccessRights.QueryStatus | ServiceAccessRights.Start);
            if (intPtr2 != IntPtr.Zero)
            {
                try
                {
                    ServiceInstaller.StartService(intPtr2);
                }
                finally
                {
                    ServiceInstaller.CloseServiceHandle(intPtr2);
                }
            }
        }
        finally
        {
            ServiceInstaller.CloseServiceHandle(intPtr);
        }
    }