locked
Error while accessing System.Management.ManagementClass RRS feed

  • Question

  • I am using the below code to get "Win32_Processor", "UniqueId" from the system.management class.

    //Return a hardware identifier
            private static string identifier(string wmiClass, string wmiProperty)
            {
                string result = "";
                System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
                System.Management.ManagementObjectCollection moc = mc.GetInstances();
                foreach (System.Management.ManagementObject mo in moc)
                {
                    //Only get the first one
                    if (result == "")
                    {
                        try
                        {
                            result = mo[wmiProperty].ToString();
                            break;
                        }
                        catch
                        {
                        }
                    }
                }
                return result;
            }

    And while running the above code getting the below exception. Strangely, this exception is coming on one of the machine and on other machines its working fine.
    The specified module could not be found. (Exception from HRESULT: 0x8007007E)
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       at System.Management.ManagementScope.InitializeGuts(Object o)
       at System.Management.ManagementScope.Initialize()
       at System.Management.ManagementObject.Initialize(Boolean getObject)
       at System.Management.ManagementClass.GetInstances(EnumerationOptions options)
       at System.Management.ManagementClass.GetInstances()
       at ConsoleApplication2.Program.identifier(String wmiClass, String wmiProperty)
       at ConsoleApplication2.Program.cpuId()
       at ConsoleApplication2.Program.Main(String[] args)


    • Edited by amical_rahul Wednesday, October 24, 2012 6:52 AM
    Wednesday, October 24, 2012 6:50 AM