[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

Select statement for ManagementObjectSearcher will not work correctly

I am trying to do a ManagementObjectSearcher search. I am using an XML file for some of the data which can be changed on the fly without having to rebuild the code everytime

This is the error I get
Event Type:     Error
Event Source:     DriveSpace
Event Category:     None
Event ID:     0
Date:          4/13/2005
Time:          12:07:46 PM
User:          N/A
Failed to get Drive Space information from Operating System.System.Management.ManagementException: Invalid query
   at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
   at System.Management.ManagementObjectEnumerator.MoveNext()
   at  c:\..bla bla\drivespace.cs:line 52

/// <Remarks> Get DriveType. </Remarks>
XmlNode xmlNodeChild = xmlNode.SelectSingleNode(TAGS.DriveTypeMonitored);
double dType = Convert.ToDouble(xmlNodeChild.InnerText);
As long as I have a actual number in the search like this "from Win32_LogicalDisk where DriveType = 3"
It will work but when I put in the dType I get the error. I would really like to
Leave this as something that is done on the fly..If you have any help please let me know

string sDriveFindings = "select * from Win32_LogicalDisk where DriveType = dType";
/// <Remarks>retrieves a collection of management objects based on query</Remarks>
     ManagementObjectSearcher DriveSearcher = new
       ManagementObjectSearcher(sDriveNamespace, sDriveFindings );
ManagementObjectCollection Collection = DriveSearcher.Get(); This is line 52***
foreach (ManagementObject Item in Collection)
1 Solution

string sDriveFindings = "select * from Win32_LogicalDisk where DriveType = " +  dType.ToString();
NewMom2BrandonAuthor Commented:
Worked Great Thank you!!!

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now