1

the following code is used to access the Win32 processor information.... is there is any other way for getting the win32 processor information (like using different classes). here i have used the class WqlObjectQuery and ManagementObjectSearcher.

WqlObjectQuery wquery = new WqlObjectQuery("select * from Win32_Processor");
        ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(wquery);
        foreach (ManagementObject mo1 in searcher1.Get())
        {
            Console.WriteLine(mo1.ToString());
        }

can we use any other classes to get the properties of win32 processor

Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
Gomathipriya
  • 905
  • 7
  • 19
  • 38
  • All depends on what information you need. I'm not used to getting this information but some basic information can be obtained through environment variables or the registry. – Nick.T Jan 30 '13 at 10:42
  • WMI can do the same thing is this .NET 4.5 specific or are you wanting to do this for .NET 3.5 ? – MethodMan Jan 30 '13 at 10:45
  • @Nick: Thanks Nick, actually my doubt is, can we use managementclass to use the query "Select * from win32_processor". – Gomathipriya Jan 30 '13 at 10:45
  • @DJ: Am using .net 3.5 framework. – Gomathipriya Jan 30 '13 at 10:45
  • is it possible to get the hardware and software information using wmi..? – Gomathipriya Jan 30 '13 at 10:50
  • There are a lot of things you can get using WMI I will post a few examples that work for you to use in a few minutes – MethodMan Jan 30 '13 at 10:52
  • Thanks DJ, i am new to WMI, your examples will surely help me – Gomathipriya Jan 30 '13 at 11:03
  • I have used it for many years and you can play around with the few code examples I have shown you.. it's a really useful thing to use. I hope this helps – MethodMan Jan 30 '13 at 11:05
  • yes it is definitely possible to get the hardware as well as software using WMI...there is a tool provided by microsoft to navigate the WMI classes and functions. The tool is Windows management Instrumentation tester. it can be opened by typing wbemtest in command prompt. You can experiment using the tool. Moreover you can check this Link and this link, and trying doing some research from your side. – Vikram Jan 30 '13 at 11:09
  • @Vikram: thanks for your information, i will try to use it. but i want to do it using .net code(c#).and your link is not visible now – Gomathipriya Jan 30 '13 at 11:17
  • all the links that I posted work not sure if you have issues reaching US links because I am not sure where you are located .. all of the code examples I have provided are in C# and prior to posting the code I have tested it personally so I know it works.. – MethodMan Jan 30 '13 at 11:22
  • @Dj: please see this code: ManagementObjectSearcher mos = new ManagementObjectSearcher(new SelectQuery("Win32_LogicalDisk")); ManagementObjectCollection moc = mos.Get(); foreach (ManagementObject mo in moc) { Console.WriteLine(mo["Name"].ToString()); Console.WriteLine(mo["version"].ToString()); Console.WriteLine(mo["csname"].ToString()); Console.WriteLine(mo["manufacturer"].ToString()); Console.WriteLine(mo["windowsdiscovery"].ToString()); } – Gomathipriya Jan 30 '13 at 11:28
  • i got the exception at the line of getting version number. – Gomathipriya Jan 30 '13 at 11:33
  • hold on one sec.. I am answering a question for another OP give me 5 mins ok – MethodMan Jan 30 '13 at 11:45
  • I fiound the error, i entered win32_logicaldisk instead of win32_processor. still am getting management exception error in csname line. – Gomathipriya Jan 30 '13 at 11:52
  • look at the first example to get the mo["Name"] I would recommend downloading the WMI Tool so that you know based on what ever query that you are running you will know which properties to get I hope this makes sense.. – MethodMan Jan 30 '13 at 12:10
  • get the tool here http://www.microsoft.com/downloads/en/details.aspx?familyid=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en it will help you in regards to understanding what you can do with WMI Query – MethodMan Jan 30 '13 at 12:16
  • Thanks DJ. I am downloading that tool. thanks a lot. – Gomathipriya Jan 30 '13 at 12:25

2 Answers2

3

Get the Current Processor name running on the Machine

Checkout the WMI Explorer as well it's a really good tool to use WMI Query Tool

MSDN WMI Queries

WMI Query Language

ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Processor")
foreach (ManagementObject mo in mos.Get())
{
    Console.WriteLine(mo["Name"]);
}

//Get Name , Manufacturer, Computer Name, etc...

ManagementObjectSearcher mosQuery = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
ManagementObjectCollection queryCollection1 = mosQuery.Get();
foreach (ManagementObject manObject in queryCollection1)
{
    Console.WriteLine("Name : " + manObject["name"].ToString());
    Console.WriteLine("Version : " + manObject["version"].ToString());
    Console.WriteLine("Manufacturer : " + manObject["Manufacturer"].ToString());
    Console.WriteLine("Computer Name : " + manObject["csname"].ToString());
    Console.WriteLine("Windows Directory : " + manObject["WindowsDirectory"].ToString());
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • DJ, i tried your code it makes an exception at run time, can you tell me why that happend...? it shows management exception. – Gomathipriya Jan 30 '13 at 11:19
  • which code / line caused the exception I get zero errors can you show me the code that you are using..? thanks – MethodMan Jan 30 '13 at 11:21
  • ManagementObjectSearcher mos = new ManagementObjectSearcher(new SelectQuery("Win32_LogicalDisk")); ManagementObjectCollection moc = mos.Get(); foreach (ManagementObject mo in moc) { Console.WriteLine(mo["Name"].ToString()); Console.WriteLine(mo["version"].ToString()); Console.WriteLine(mo["csname"].ToString()); Console.WriteLine(mo["manufacturer"].ToString()); Console.WriteLine(mo["windowsdiscovery"].ToString()); } – Gomathipriya Jan 30 '13 at 11:23
  • @Gomathipriya you're getting an exception because the [Win32_LogicalDisk object](http://msdn.microsoft.com/en-us/library/windows/desktop/aa394173(v=vs.85).aspx) has no "version" property. Or csname, manufacturer, or windowsdiscovery for that matter. Always check if a property is null before using it. – SWalters Jun 18 '13 at 14:53
1

yes it is definitely possible to get the hardware as well as software using WMI...there is a tool provided by microsoft to navigate the WMI classes and functions. The tool is Windows management Instrumentation tester. it can be opened by typing wbemtest in command prompt. You can experiment using the tool. Moreover you can check this Link and this link, and trying doing some research from your side.

Apart from all this you can use the query as "select * from Win32_Processor where DeviceID=CPU0" as you want to get the information about single processor

Vikram
  • 1,617
  • 5
  • 17
  • 37