title: How to use ManagementEventWatcher in a windoes service?
body: <p>I am trying to use ManagementEventWatcher inside a C# .NET4 windows service application. At some point in the "OnStart" method I have these lines:</p>
  
  <pre><code>string scope = "\\root\\cimv2";
  string query =
    "SELECT * FROM __InstanceCreationEvent " +
    "WITHIN 1 " +
    "WHERE TargetInstance ISA 'Win32_NTLogEvent' " +
    " AND ( " +
    "      targetInstance.EventCode = '4768' " + 
    "   OR targetInstance.EventCode = '4770' " + 
    "   OR targetInstance.EventCode = '672'" + 
    "   OR targetInstance.EventCode = '674'" + 
    " )";
  eventListener = new ManagementEventWatcher(scope, query);
  eventListener.EventArrived += new EventArrivedEventHandler(HandleEvent);
  
  
  eventListener.Start();
  </code></pre>
  
  <p>The eventListener.Start(); line gives an exception with the message "Access Denied". I tried to set up my service as Administrator or LOCALSYSTEM but it does not help.</p>
  
  <p>I also found this documentation: <a href="http://msdn.microsoft.com/en-us/library/aa392289(VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa392289(VS.85).aspx</a>
  But I can not figure out how to use it in C#.</p>
  
  <p>The same code works as expected if put in a WindowsForms application.</p>
  
  <p>Thanks.</p>