Solved

c# does not recognize WMI class

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

803 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