0

I am trying to create a lightweight application that will check certain security settings on a machine. Currently, I have everything except for the current encryption status. I was lead towards the WMI creator which seems to be the safest bet (all be it a little slow) However, I am getting the error below:

"foreach statement cannot operate on variables of type 'ComplianceGuide.ManagementObjectCollection' because 'ComplianceGuide.ManagementObjectCollection' does not contain a public definition for 'GetEnumerator'

Where do i go to add the definition? C# is so confusing sometimes. A video tutorial would be AWESOME.

enter image description here

    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);
        }
    }
}
Joe Pearson
  • 105
  • 10
  • 1
    "Where do i go to add the definition?" You add it to the class - either directly or by implementing `IEnumerable` or `IEnumerable`. – D Stanley May 25 '16 at 17:37
  • "A video tutorial would be AWESOME" - [others think so too](https://www.google.com/search?q=c%23+video+tutorials) – D Stanley May 25 '16 at 17:38
  • @DStanley it seems as if Joe is using a standard class (https://msdn.microsoft.com/en-us/library/system.management.managementobjectcollection(v=vs.110).aspx) that supposedly already implements IEnumerable. – Gilad Naaman May 25 '16 at 17:52
  • @GiladNaaman The `ComplianceGuide` namespace seems to indicate otherwise. – D Stanley May 25 '16 at 17:53
  • Are you trying to use the built-in `ManagementObjectCollection` class or did you define your own in a `ComplianceGuide` namespace? – D Stanley May 25 '16 at 17:55
  • @DStanley I thought so too, but he's also using ManagementObjectSearcher(https://msdn.microsoft.com/en-us/library/system.management.managementobjectsearcher(v=vs.110).aspx) and I doubt he wrote both of these classes from scratch. – Gilad Naaman May 25 '16 at 17:56
  • @GiladNaaman Well I can't see the image (blocked) but based on the question text the error is from a different namespace. – D Stanley May 25 '16 at 17:57
  • @JoePearson Can you please edit your post to include the textual code? – Gilad Naaman May 25 '16 at 17:58
  • http://stackoverflow.com/questions/30673984/wmi-code-creator-for-bitlocker-status-where-did-i-go-wrong – Slai May 25 '16 at 18:09

1 Answers1

1

I see a file that you have open in your screenshot called ManagementObjectCollection.cs.

I'm going to go out on a limb and guess that for some reason the namespace definition for that class matches the exact namespace definition for the framework provided class, and whatever is in that file does not implement GetEnumerator. That code in a fresh project and the proper references does not show that error.

If you intended to use the framework provided classes, my guess is you need to add a reference to System.Management.dll. If you intended to use the class in your file, change the namespace at the very least.

plast1k
  • 793
  • 8
  • 15
  • I changed the NameSpace a while back and honestly couldn't tell you why. However, I get a decent amount of errors if I try switching to the System.Management namespace. – Joe Pearson May 25 '16 at 18:12
  • How can I add a reference to the IEnumerator for the foreach definition to my current NameSpace? I feel like I would be better off give that this is the last piece of functionality that I need to be done with this project. – Joe Pearson May 25 '16 at 18:13
  • Don't change the namespace on _your_ class - _reference the `System.Management` library_. It looks like you've copied source code form that library which is not what you should have done. – D Stanley May 25 '16 at 18:14
  • This is honeslty the first time trying to complete a project on my own, I'm still only a few weeks into learning C#. Sorry :/ – Joe Pearson May 25 '16 at 18:15
  • No need to be sorry.The namespaces you define for you code should not match any framework namespaces. You end up with a conflict where the compiler doesnt know which class youre talking about. I might take a step back and do some reading on C# basics. https://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx – plast1k May 25 '16 at 18:16
  • It's all part of the learning process. So if I were to change the namespace to System.Management where would I go to accomplish this? I suppose I could go back through all the other errors and reference them back to the ProperNamespace and add whatever references are missing. – Joe Pearson May 25 '16 at 18:25
  • No, you don't use _source code_ to use the System.Management classes, you _reference the `System.Management` assembly_. You should not have any source code for `ManagementObjectCollection` in any namespace anywhere in your project. – D Stanley May 25 '16 at 21:47
  • Here's what I dont understand. I show that System.Management is checked in the Solution Explorer. Where am i supposed to go to add System.Management to the namespace ComplaiceGuide? – Joe Pearson May 25 '16 at 21:55