Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / April 2007

Tip: Looking for answers? Try searching our database.

Access WMI in C# ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Henry - 07 Apr 2007 19:58 GMT
Below is the code I am trying to use in a console application, each time I
run it I get an Management error Illegal Class error.  I have traced the
error to the \"Win32_Process\" line, apparently the wmi path is incorrect.
Tried to run the app on both xp and vista with the same error.  Could
someone suggest how I might correct this wmi path variable???

Thanks

Henry

+++++++++++++++++++++++++++++++++++++++++++++++++++++

public static void WriteToEventLog()
       {
           WqlEventQuery DemoQuery = new
WqlEventQuery("_InstanceCreationEvent", new TimeSpan(0, 0, 1),
"TargetInstance isa \"Win32_Process\" ");

           ManagementEventWatcher DemoWatcher = new
ManagementEventWatcher();
           DemoWatcher.Query = DemoQuery;
           DemoWatcher.Options.Timeout = new TimeSpan(0, 0, 30);

           Console.WriteLine("Open an application to trigger an Event.");

           ManagementBaseObject e = DemoWatcher.WaitForNextEvent();

           EventLog DemoLog = new EventLog("Chap10Demo");
           DemoLog.Source = "Chap10Demo";
           String EventName =
((ManagementBaseObject)e["TargetInstance"])["Name"].ToString();
           Console.WriteLine(EventName);
           DemoLog.WriteEntry(EventName, EventLogEntryType.Information);

           DemoWatcher.Stop();
       }

+++++++++++++++++++++++++++++++Error follows

Open an application to trigger an Event.

Unhandled Exception: System.Management.ManagementException: Invalid class
  at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStat
us errorCode)
  at System.Management.ManagementEventWatcher.WaitForNextEvent()
  at ManagementEvents1.Program.WriteToEventLog() in
I:\Projects\ManagementEvent
s1\ManagementEvents1\Program.cs:line 29
  at ManagementEvents1.Program.Main(String[] args) in
I:\Projects\ManagementEve
nts1\ManagementEvents1\Program.cs:line 14
Press any key to continue . . .

Signature

                                ~
Most good judgment comes from experience.
Most experience comes from bad judgment.
           ~I'm leaning towards bad judgment.~

Willy Denoyette [MVP] - 09 Apr 2007 14:44 GMT
> Below is the code I am trying to use in a console application, each time I
> run it I get an Management error Illegal Class error.  I have traced the
[quoted text clipped - 46 lines]
> nts1\ManagementEvents1\Program.cs:line 14
> Press any key to continue . . .

Notice the space at the end of ..
"TargetInstance isa \"Win32_Process\" ");

should be...
"TargetInstance isa \"Win32_Process\"");

Willy.
Chris Mullins [MVP] - 09 Apr 2007 19:43 GMT
[Willy doing WMI Stuff]

You always have the coolest WMI stuff handy. Have you written a FAQ on it
anywhere, or archived all your examples?

Signature

Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

Willy Denoyette [MVP] - 10 Apr 2007 10:53 GMT
> [Willy doing WMI Stuff]
>
> You always have the coolest WMI stuff handy. Have you written a FAQ on it anywhere, or
> archived all your examples?

Chris, a have an archive of samples, I just have to clean them up and add some comments, I
will save some time to do this and keep you informed here.
As for the FAQ, I keep some inter-company kind of Knowledge Base articles, but I'm afraid I
can't publish them as they are, I will see what I can do to turn this stuff into some
publically available FAQ .

Willy.
Henry - 10 Apr 2007 03:35 GMT
Well, I wish it was that easy, corrected the space as you suggested, here is
the error...
It really seems to be a problem with the watcher, but what do I know???

Henry

Open an application to trigger an Event.

Unhandled Exception: System.Management.ManagementException: Invalid class
  at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStat
us errorCode)
  at System.Management.ManagementEventWatcher.WaitForNextEvent()
  at ManagementEvents1.Program.WriteToEventLog() in
I:\Projects\ManagementEvent
s1\ManagementEvents1\Program.cs:line 29
  at ManagementEvents1.Program.Main(String[] args) in
I:\Projects\ManagementEve
nts1\ManagementEvents1\Program.cs:line 14
Press any key to continue . . .

>> Below is the code I am trying to use in a console application, each time
>> I
[quoted text clipped - 59 lines]
>
> Willy.
Willy Denoyette [MVP] - 10 Apr 2007 10:41 GMT
> Well, I wish it was that easy, corrected the space as you suggested, here is the error...
> It really seems to be a problem with the watcher, but what do I know???
[quoted text clipped - 12 lines]
> nts1\ManagementEvents1\Program.cs:line 14
> Press any key to continue . . .

Sorry missed this one...

_InstanceCreationEvent
should be:
__InstanceCreationEvent

notice the double underscore!

Willy.
majlathm@gmail.com - 10 Apr 2007 09:14 GMT
> Below is the code I am trying to use in a console application, each time I
> run it I get an Management error Illegal Class error.  I have traced the
[quoted text clipped - 55 lines]
> Most experience comes from bad judgment.
>             ~I'm leaning towards bad judgment.~

If you want to play around with WMI (find right syntax etc.) try to
use WMI Creator:

http://www.microsoft.com/downloads/details.aspx?familyid=2cc30a64-ea15-4661-8da4
-55bbc145c30e&displaylang=en


Marian
Henry - 10 Apr 2007 17:19 GMT
Yes, thanks for the excellent help, and I did find the WMI Creator, it is a
great help for finding proper syntax...

Again Thanks

Henry

>> Below is the code I am trying to use in a console application, each time
>> I
[quoted text clipped - 65 lines]
>
> Marian

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.