.NET Web应用程序无法读取当前正在运行的服务

时间:2016-07-05 16:08:04

标签: c# .net wmi

我有一些代码试图获取当前正在运行的服务的列表,并从该列表中找到特定的服务。

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;每个人"访问权限以及启动和激活权限,但这不会改变任何内容。

1 个答案:

答案 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);
}