Tim*_*ird 6 c# registry service
是否有.NET(C#)方法或API调用,我可以用来查询Windows服务是否被禁用?相关的MSDN文章就在这里.
我想避免直接查询注册表.下面是我现在正在使用的一些代码(并且它可以工作).然而,我正在寻找更优雅,更少侵入性的东西.
const String basepathStr = @"System\CurrentControlSet\services\";
String subKeyStr = basepathStr + servicenameStr;
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(subKeyStr))
{
return (int) key.GetValue("Start");
}
Run Code Online (Sandbox Code Playgroud)
我确实找到了一个简短的问题,但我希望得到一个更好的答案,因为答案可能已经过时(3年过去了).
这是我决定使用的代码中最相关的部分...感谢所有人的帮助!
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11693 次 |
| 最近记录: |