1

enter image description hereI am working with the WMI Code Creator and the code looks to work properly from the app. HOWEVER, It comes up with errors internal of my code that I cant seem to shake. Am I supposed to have a reference for this to work? if so where can I get it?

    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2\\Security\\MicrosoftVolumeEncryption",
                    "SELECT * FROM Win32_EncryptableVolume");

                foreach (System.Management.ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_EncryptableVolume instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("ProtectionStatus: {0}", queryObj["ProtectionStatus"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }

enter image description here
(click for larger image)

Joe Pearson
  • 149
  • 1
  • 2
  • 13
  • I doubt anybody could tell you what a "ComplianceGuide" might be. Sounds like you've been wildly making chances to try to solve the problem. Which is a very simple one, ensure you've added a reference to System.Management and put `using System.Management;` at the top of the file. – Hans Passant Jun 05 '15 at 20:37
  • this is a spot of code I found through another website that had never been resolved. the ComplianceGuide is simply the app namespace, it has been altered in the code to fit. Thanks for spotting that. – Joe Pearson Jun 07 '15 at 06:36

1 Answers1

3

If you search the MSDN for ManagementObjectSearcher you get this page. On every MSDN page for a .NET class you will see two pieces of information at the top of the page.

Namespace: System.Management
Assembly: System.Management (in System.Management.dll)

The first line tells you that you need to add using System.Management; or do System.Management.ManagementObjectSearcher if you want to reference the object.

The second line tells you your project must reference the file System.Management.dll for your code to be able to find the class. If you search in the Add Reference Dialog you need to look for System.Management (the part outside of the parenthesis) in the list.

enter image description here


The second part of your problem is you have a class called ComplianceGuide.ManagmentObject in your project and Visual Studio is picking that reference up instead of System.Management.ManagementObject, replace your foreach with

foreach (System.Management.ManagementObject queryObj in searcher.Get())

to force it to use the correct class.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • Okay I got all the errors out except one. which is the "foreach" apparently I have no definition for the 'GetEnumerator'. How can I fix this? – Joe Pearson Jun 08 '15 at 16:17
  • Don't know, `ManagementObjectCollection` does implement `IEnumerable` so the error should not be happening. I would ask a new question with the updated code asking why the foreach does not work. – Scott Chamberlain Jun 08 '15 at 16:29
  • I added info above including an image of the error. Thus far I have added the references and changed the Foreach line as you had suggested. – Joe Pearson Jun 08 '15 at 16:38
  • From your image you can see `searcher` is a `ComplianceGuide.ManagementObjectSearcher` not a `System.Management.ManagementObjectSercher`. I don't know why you re-created all of the management classes but those naming conflicts are your problem. Replace the declaration of searcher with `System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(...` – Scott Chamberlain Jun 08 '15 at 16:48
  • Little Side question. How can I take this console data and display the read value in a textbox? – Joe Pearson Jun 08 '15 at 20:15
  • The answer to that is a lot more than is the scope of a question comment, try to do it yourself and if you get stuck ask a new question. However, if you are wrting this program as a solution to [this other question of yours](http://stackoverflow.com/questions/30654985/is-there-a-method-to-open-cmd-as-admin-through-a-c-sharp-command) you should not send data through the console. Look in to some other form of interprocess communication like WCF which can cross the Admin/Non-Admin boundry. – Scott Chamberlain Jun 08 '15 at 20:29
  • Im guessing this is because we cant pass admin from our application to console? therefore we cant access these admin levels and pass them to a form? I will play with it today and see what I come up with. – Joe Pearson Jun 08 '15 at 20:48
  • Correct, see my answer to your other question for a link to a simple example of how to do WCF over named pipes. – Scott Chamberlain Jun 08 '15 at 21:04