Querying if a Windows Service is disabled (without

2019-01-24 13:27发布

Is there a .NET (C#) method or API call that I can use to query if a Windows Service is disabled? The relevant MSDN article is here.

I want to avoid querying the registry directly. Below is some of the code that I am using right now (and it works). However I am looking for something more elegant and less invasive.

const String basepathStr = @"System\CurrentControlSet\services\";
String subKeyStr = basepathStr + servicenameStr;

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(subKeyStr))
{
    return (int) key.GetValue("Start");
}

I did find a simliar question but I was hoping for a better answer since the answers are presumably outdated (3 years have passed).

6条回答
萌系小妹纸
2楼-- · 2019-01-24 13:53

Use the ServiceController class to get information about services.

EDIT
Seems one of the things you can't do with the ServiceController is get the startup type. Googling showed the following blog post that has code that uses P/Invoke to get the service startup type: http://peterkellyonline.blogspot.de/2011/04/configuring-windows-service.html

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-24 14:02

Add a ref to System.Management and the following code will get you the StartMode

        string wmiQuery = "SELECT * FROM Win32_Service WHERE Name='YourServiceName'";
        var searcher = new ManagementObjectSearcher(wmiQuery);
        var results = searcher.Get();

        foreach (ManagementObject service in results)
        {
            Console.WriteLine(service["StartMode"]);
        }
查看更多
乱世女痞
4楼-- · 2019-01-24 14:06

This the most relevant section of the code I decided to use...thanks for the help all!

    StartupState state = StartupState.Unknown;
    try
    {
        PermissionSet fullTrust = new PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
        fullTrust.Demand();
        string wmiQuery = @"SELECT * FROM Win32_Service WHERE Name='" + servicenameStr + @"'";
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
        ManagementObjectCollection results = searcher.Get();
        foreach (ManagementObject service in results)
        {
            if (service["StartMode"].ToString() == "Disabled")
                state = StartupState.Disabled;
            else
                state = StartupState.Enabled;
        }
        return state;
    }
    catch (SecurityException se)
    {
        return StartupState.Refused;
    }
    catch (Exception e)
    {
        return StartupState.Error;
    }
查看更多
萌系小妹纸
5楼-- · 2019-01-24 14:06

You can use:

using System.ServiceProcess;

And then link the service you want to view the satus by:

// Link by service name
ServiceController TheServiceName = new ServiceController();
TheServiceName.ServiceName = "Spooler";

// Link by display name
ServiceController TheDisplayName = new ServiceController();
TheDisplayName.ServiceName = "Print Spooler";

To check for example the isRunning Status :

if (TheServiceName.Status == ServiceControllerStatus.Running)
    MessageBox.Show("The service is running.");
查看更多
你好瞎i
6楼-- · 2019-01-24 14:09

ServiceController class doesn't provide this information. You should use WMI. See here for detailed solution

查看更多
看我几分像从前
7楼-- · 2019-01-24 14:18

WMI can be another way for querying the status of the windows services

查看更多
登录 后发表回答