<p>I've having trouble directly accessing the <strong>Win32_OperatingSystem</strong> management class that is exposed via WMI.</p>

<p>It is a singleton class, and I'm pretty certain "Win32_OperatingSystem=@" is the correct path syntax to get the instance of a singleton.</p>

<p>The call to InvokeMethod produces the exception listed at the bottom of the question, as does accessing the ClassPath property (commented line).</p>

<p>What am I doing wrong?</p>

<p>[I'm aware that I can use ManagementObjectSearcher/ObjectQuery to return a collection of Win32_OperatingSystem (which would contain only one), but since I know it is a singleton, I want to access it directly.]</p>

<hr>

<pre><code>ManagementScope cimv2 = InitScope(string.Format(@"\\{0}\root\cimv2", this.Name));

ManagementObject os = new ManagementObject(
    cimv2,
    new ManagementPath("Win32_OperatingSystem=@"),
    new ObjectGetOptions());

//ManagementPath p = os.ClassPath;

os.InvokeMethod("Reboot", null);
</code></pre>

<hr>

<p>System.Management.ManagementException was caught
  Message="Invalid object path "
  Source="System.Management"
  StackTrace:
  at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
  at System.Management.ManagementObject.Initialize(Boolean getObject)
  at System.Management.ManagementBaseObject.get_wbemObject()
  at System.Management.ManagementObject.get_ClassPath()
  at System.Management.ManagementObject.GetMethodParameters(String methodName, ManagementBaseObject&amp; inParameters, IWbemClassObjectFreeThreaded&amp; inParametersClass, IWbemClassObjectFreeThreaded&amp; outParametersClass)
  at System.Management.ManagementObject.InvokeMethod(String methodName, Object[] args)</p>

<hr>

<p>Thanks for the replies.</p>

<p><strong>Nick</strong> - I don't know how to go about doing that :)</p>

<p><strong>Uros</strong> - I was under the impression that it was a singleton class because of <a href="http://msdn.microsoft.com/en-us/library/aa394239.aspx" rel="nofollow noreferrer">this</a> MSDN page. Also, opening the class in the WBEMTest utility shows <a href="http://img247.imageshack.us/img247/5686/64933271au3.png" rel="nofollow noreferrer">this</a>.</p>

<hr>

<p>The instances dialog shows: "1 objects" and "max. batch: 1" in those fields and lists "Win32_OperatingSystem=@"</p>

<p>The ManagementScope is verified as working, so I don't know what's up. I'm a WMI novice, but this seems like one of the simplest use cases!</p>

[{'answer_id': 57808, 'author': 'Nick Randell', 'author_id': 5932, 'author_profile': 'https://Stackoverflow.com/users/5932', 'pm_score': 1, 'selected': False, 'text': "<p>I'm not 100% sure of the answer, but have you tried using reflector to look at what ManagementObjectSearcher does? It may give you some clue as to what you are doing wrong.</p>\n"}, {'answer_id': 65735, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Win32_OperatingSystem is not a singleton class - if you check its qualifiers, you\'ll see that there is no Singleton qualifier defined for it, so you\'ll have to use ManagementObjectSearcher.Get() or ManagementClass.GetInstances() even though there is only one instance of the class. Win32_OperatingSystem key property is Name, so there is an option to get the instance directly, using </p>\n\n<pre><code>ManagementObject OS = new ManagementObject(@"Win32_OperatingSystem.Name=\'OSname\'")\n</code></pre>\n\n<p>but in my experience, OSName is always something like:</p>\n\n<p>"Microsoft Windows XP Professional|C:\\WINDOWS|\\Device\\Harddisk0\\Partition1"</p>\n\n<p>so using ManagementObjectSearcher is probably the easiest solution.</p>\n'}, {'answer_id': 73199, 'author': 'dcstraw', 'author_id': 10391, 'author_profile': 'https://Stackoverflow.com/users/10391', 'pm_score': 1, 'selected': False, 'text': "<p>I would probably construct a query that gets the instance where Primary = true.  I haven't used Win32_OperatingSystem in a while, but I seem to remember getting multiple instances, and the one that was currently booted had Primary equal to true.</p>\n"}, {'answer_id': 74863, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>Duncan wrote:</p>\n  \n  <p>The instances dialog shows: "1 objects" and "max. batch: 1" in those fields and >lists "Win32_OperatingSystem=@"</p>\n</blockquote>\n\n<p>It sure looks like it should work. You could test your code with another singleton class, like:</p>\n\n<p>"Win32_WmiSetting=@"</p>\n\n<p>and see if you still get the exception.</p>\n'}, {'answer_id': 90709, 'author': 'Nick Randell', 'author_id': 5932, 'author_profile': 'https://Stackoverflow.com/users/5932', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve just tried this simple app that worked ok</p>\n\n<pre><code>using System;\nusing System.Management;\n\nnamespace WmiPlay\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            try\n            {\n                ManagementScope cimv2 = new ManagementScope(@"\\\\.\\root\\cimv2");\n                ManagementObject os = new ManagementObject(cimv2, new ManagementPath("Win32_OperatingSystem=@"), new ObjectGetOptions());\n                Console.Out.WriteLine(os);\n            }\n            catch (Exception ex)\n            {\n                Console.Error.WriteLine(ex);\n            }\n        }\n    }\n}\n</code></pre>\n\n<p>See if this works for you? I did run it in Visual Studio which I normally run as administrator under Vista x64.</p>\n'}]
['https://Stackoverflow.com/questions/56208', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/82/']