Solved

c# does not recognize WMI class

Posted on 2011-03-06
4
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

762 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