I used this code for finding graphic cards:
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_VideoController");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
string graphicsCard = "";
foreach (ManagementObject mo in queryCollection)
{
foreach (PropertyData property in mo.Properties)
{
if (property.Name == "Description")
{
graphicsCard += property.Value.ToString() + " ";
}
}
}
I have two graphic cards:
Above code return all graphic cards.
How to find active graphic card that chosen by windows?
try this
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
string graphicsCard = string.Empty;
foreach (ManagementObject obj in searcher.Get())
{
if (obj["CurrentBitsPerPixel"] != null && obj["CurrentHorizontalResolution"] != null)
{
graphicsCard = obj["Name"].ToString();
}
}
Related
Here is my list:
public static List<Tuple<string, string>> hardDiskInfo(string hostname)
{
var hardDiskInfo = new List<Tuple<string, string>>();
ManagementScope Scope;
if (!hostname.Equals("localhost", StringComparison.OrdinalIgnoreCase))
{
ConnectionOptions Conn = new ConnectionOptions();
Conn.Username = Properties.Settings.Default.uName;
Conn.Password = Properties.Settings.Default.pWord;
Conn.Authority = "ntlmdomain:" + Properties.Settings.Default.doMain;
Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", hostname), Conn);
}
else
Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", hostname), null);
Scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3 OR DriveType = 4");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(Scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject mo in queryCollection)
{
foreach (PropertyData p in mo.Properties)
{
if (p.Value != null)
{
hardDiskInfo.Add(new Tuple<string, string>(p.Name.ToString(), p.Value.ToString()));
}
}
}
return hardDiskInfo;
}
I'd like to know how to get the second p.Value of the p.Name after calling it:
hardDiskInfo(inputText.Text);
For example the value of the "FreeSpace" which is defined in Win32_LogicalDisk.
I'm having more Win32_ queries so knowing this will help me handling all of them and I'll be a happy panda.
Thank you.
Are the Names unique?
You might try:
var values = hardDiskInfo(inputText.Text);
// Get the first or default which matches "FreeSpace".
var freeSpaceInfo = values.FirstOrDefault(item => item.Item1 == "FreeSpace");
// If it was found,
if(freeSpaceInfo != null)
{
MessageBox.Show($"FreeSpace: {freeSpaceInfo.Item2}");
}
Next step: Use a Dictionary<string, string> which is much better.
OK, so this drives me nuts.
The code below worked just fine in Windows 7 with .NET 3.5.
In Windows 8.1 and .NET 4.5.1 I get an empty result, but using the WMI Code Creator I can get the results.
I cannot find anything about this online.
I want to get the friendly names of any COM ports, e.g. "Communications Port (COM1)".
Just using System.IO.Ports.SerialPort.GetPortNames() wont do.
I really hope someone know how to do this. Thanks!
using System;
using System.Collections.Generic;
using System.Management;
namespace OakHub
{
public class SerialMgmt
{
static public List<String> GetCOMDevices()
{
List<String> list = new List<String>();
ManagementScope scope = new ManagementScope(#"\\" + Environment.MachineName + #"\root\CIMV2");
SelectQuery sq = new SelectQuery("SELECT Caption FROM Win32_PnPEntity");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, sq);
ManagementObjectCollection moc = searcher.Get();
foreach (ManagementObject mo in moc)
{
String name = mo.ToString();
if (name.ToString().Contains("(COM"))
{
list.Add(name);
}
}
return list;
}
}
}
First of all I dont know why this code is even working for you (with .Net 3.5).
You just selected the property Caption. (Use * to select all, if needed)
I think you want the name of the Win32_PnPEntity-Devices, you cant get it with this line of code
String name = mo.ToString();
Because the Name is a property. You First have to load the Property with the WMI-String :
SELECT Name,Caption FROM Win32_PnPEntity //Get Name and Caption Property
or
SELECT * FROM Win32_PnPEntity //Load all the Propertys of that WMI-Obj
And than you have to check if value is null else --> return the value
Code:
public List<String> GetLocalCOMDevices()
{
List<String> list = new List<String>();
ManagementScope scope = new ManagementScope(#"\\" + Environment.MachineName + #"\root\CIMV2");
SelectQuery sq = new SelectQuery("SELECT Name,Caption FROM Win32_PnPEntity");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, sq);
ManagementObjectCollection moc = searcher.Get();
foreach (ManagementObject mo in moc)
{
object propName = mo.Properties["Name"].Value;
if (propName == null) { continue; }
list.Add(propName.ToString());
}
return list;
}
I'm trying to get some info about RSOP_SecuritySettingBoolean but it returns an empty collection. Am i doing something wrong? Win7 x64 HP without domain:
var options = new ConnectionOptions();
var scope = new ManagementScope(#"\\.\root\RSOP\Computer", options);
var objectQuery = new ObjectQuery("SELECT * FROM RSOP_SecuritySettingBoolean");
using (var searcher = new ManagementObjectSearcher(scope, objectQuery))
{
foreach (ManagementObject o in searcher.Get())
{
Console.WriteLine("Key Name: {0}", o["KeyName"]);
Console.WriteLine("Precedence: {0}", o["Precedence"]);
Console.WriteLine("Setting: {0}", o["Setting"]);
}
}
Is it possible get it using WMI query?
my current code:
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_NetworkAdapte");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine(queryObj[??]);
}
I'm tried get the connections name from:
Control Panel \ Network and Internet \ Network Connections
Using code below you would be able dump all properties of the Network Adapter, you need Name property:
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_NetworkAdapter");
foreach (ManagementObject adapter in searcher.Get())
{
StringBuilder propertiesDump = new StringBuilder();
foreach (var property in adapter.Properties)
{
propertiesDump.AppendFormat(
"{0} == {1}{2}",
property.Name,
property.Value,
Environment.NewLine);
}
}
OR simply using LINQ (add using System.Linq):
foreach (ManagementObject adapter in searcher.Get())
{
string adapterName = adapter.Properties
.Cast<PropertyData>()
.Single(p => p.Name == "Name")
.Value.ToString();
}
PS: Also be aware you've typo in WMI query - forgot r in Adapter: Win32_NetworkAdapte_r_
Let's change the code a little more and shorten it. ManagementClass class shortens the WMI query.
ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
ManagementObjectCollection results = mc.GetInstances();
foreach (var result in results)
{
foreach (PropertyData data in result.Properties)
Console.WriteLine($"{data.Name} == {data.Value}");
}
This is my code, I can get name, description...
ManagementClass MgmtClass = new ManagementClass("Win32_SystemDriver");
foreach (ManagementObject mo in MgmtClass.GetInstances())
{
name=mo["Name"];
Dis=mo["Description"];
...
}
How can I get the date and version of drivers?
You should start from researching Win32_PnPSignedDriver Class and Win32_PnPEntity Class
EXAMPLE
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_PnPSignedDriver");
ManagementObjectCollection moc = searcher.Get();
foreach (var manObj in moc)
{
Console.WriteLine("Device Name: {0}\r\nDeviceID: {1}\r\nDriverDate: {2}\r\nDriverVersion: {3}\r\n==============================\r\n", manObj["FriendlyName"], manObj["DeviceID"], manObj["DriverDate"], manObj["DriverVersion"]);
}