我有一些代码试图获取当前正在运行的服务的列表,并从该列表中找到特定的服务。
using (ManagementClass mc = new ManagementClass("Win32_Service"))
{
var t = mc;
var g = mc.GetInstances();
// iterate through services until IMSAgent is found.
foreach (ManagementObject mo in mc.GetInstances())
{
if (mo.Properties["Name"].Value.ToString() == "String")
{
// get path name.
PropertyData pd = mo.Properties["PathName"];
ret = String.Concat(pd.Value.ToString(), ".config");
// break.
break;
}}
此代码会在mc.GetInstances()
Access denied
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
at **.GetIMSAgentConfigFilePath()
at **.GetSettings(SqlConnection cn)
at **.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
我一直在谷歌搜索WMI权限错误,DCOMCON权限错误等不同的事情。我尝试了很多东西,比如给予#34;每个人"访问权限以及启动和激活权限,但这不会改变任何内容。
答案 0 :(得分:1)
ServiceController[] services = ServiceController.GetServices();
foreach (var service in services)
{
string imagePath = Microsoft.Win32.Registry.LocalMachine.GetValue(@"SYSTEM\CurrentControlSet\Services\" + service.ServiceName + "\ImagePath");
Console.WriteLine("service " + service.ServiceName + " = " + imagePath);
}