Solved

c# does not recognize WMI class

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is Thread.Sleep? 5 46
Stream.BeginRead and Stream.EndRead in .NET Core 5 37
consuming an asmx web service in winforms application 3 24
Connection String 16 43
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

920 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

17 Experts available now in Live!

Get 1:1 Help Now