简体   繁体   中英

.NET Web application fails to read currently running Services

I have some code that tries to get a list of currently running services and find a specific one off of that list.

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

This code throws this error at 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)

I have been Googling about different things like WMI permissions error,DCOMCON permissions error. I have tried many things like giving "Everyone" access permissions and Launch and activation permissions but this does not change anything.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM