Request if the Windows service is disabled (without using the registry)?

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

I want to avoid a direct registry request. Below is the code I'm 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 found a simliar question , but I was hoping for a better answer as the answers are supposedly out of date (3 years have passed).

+6
source share
6 answers

This is the most important section of code that I decided to use ... thanks for helping everyone!

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

Use the ServiceController class to get information about services.

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

+3
source

WMI may be another way to request the status of Windows services

0
source

Add a link to System.Management and the following code will provide you with 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"]); } 
0
source

The ServiceController class does not provide this information. You must use WMI. See here for a detailed solution.

0
source

You can use:

 using System.ServiceProcess; 

And then bind the service you want to view with:

 // 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, isRunning Status:

 if (TheServiceName.Status == ServiceControllerStatus.Running) MessageBox.Show("The service is running."); 
-3
source

Source: https://habr.com/ru/post/914536/


All Articles