Solved

c# does not recognize WMI class

Posted on 2011-03-06
4
500 Views
Last Modified: 2013-12-17
hi guys,

i'm having trouble to run a testing code that i found on internet.

it involves using WMI to query localhost information
somehow, my vs2010 ultimate seems not recognize CLASS that suppose to  be in SYSTEM.MANAGEMENT

e.g   compiler gives error on ManagementClass  which is listed on msdn class directory.


im using win7x64 ultimate and VS2010 ultimate.

can someone please help?
thanks


using System;
using System.Linq;
using System.Text;
using System.Management;
using System.Management.Instrumentation;

namespace ConsoleApplication2
{
   
public class QueryInstances {
    public static int Main(string[] args) {
      // create management class object
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
//collection to store all management objects
ManagementObjectCollection moc = mc.GetInstances();
if(moc.Count != 0)
{
    foreach (ManagementObject mo in mc.GetInstances())
    {
        // display general system information
        Console.WriteLine("\nMachine Make: {0}\nMachine Model: {1}\nSystem Type: {2}\nHost Name: {3}\nLogon User Name: {4}\n",
                          mo["Manufacturer"].ToString(),
                          mo["Model"].ToString(),
                          mo["SystemType"].ToString(),
                          mo["DNSHostName"].ToString(),
                          mo["UserName"].ToString());
    }
}
//wait for user action
Console.ReadLine();

      }
    
    }
}
    }

Open in new window

0
Comment
Question by:Ikelca
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35046326
You need to add a reference to the "System.Management" assembly if you haven't already. Right-click your project choose "Add Reference". In the dialog that opens switch to the .Net tab and scroll down until you find "System.Management". Select it and click OK.
0
 

Author Comment

by:Ikelca
ID: 35046722
great, that works, but how come "using System.Management;" does not work??
so i guess it's different from java?
then how do i know when to add reference?
0
 

Author Comment

by:Ikelca
ID: 35046797
i meant i thought adding "using System.Management" invoke class already...
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35046906
Adding "using System.Management" just includes the namespace into the current file. The System.Management namespace covers classes in a couple of different assemblies. Some of the classes in the namespace get included by default, whereas some of the additional classes require an extra assembly reference.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now