Solved

c# does not recognize WMI class

Posted on 2011-03-06
4
507 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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!
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

623 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