retrieving WMI query information in uint16 format c# - c#

I've been trying to retrieve information about remote computers in our network via a WMI query. This works fine for all the info I need, I just don't seem to get the UserFriendlyName from the WmiMonitorID class. As this value is stored as a uint16.
The code below returns System.UInt16[]
But I would like to get some readable information.
This is the method I use to retrieve the information:
GetDirectWmiQuery("UserFiendlyName", "WmiMonitorID");
public static string GetDirectWmiQuery(string item, string table)
{
string result = string.Empty;
ManagementScope scope;
scope = new ManagementScope($"\\\\{Var.hostnm}\\root\\WMI");
scope.Connect();
ObjectQuery query = new ObjectQuery($"Select {item} FROM {table}");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
result += m[item].ToString();
}
return result;
}

Translated to C# based on the excellent answer from Jimi,
The returned array needs to be converted to a string, to become human-eye-friendly. The UInt16 byte array can be converted with Convert.ToByte(UInt16), then tranformed into string with Encoding.GetString().
foreach (ManagementObject m in queryCollection)
{
var data = (ushort[])m[item];
var monitorID = Encoding.UTF8.GetString((data).Select(Convert.ToByte).ToArray());
result+= monitorID + "\n";
}

Related

Using array to display Win32_SystemDriver query results in C#

So I am trying to get Description information from Win32_SystemDriver into a RichTextBox, but I am not able to do that because it only displays the last result from the query. As you can see bellow I tried to build an array but it does not work.
ObjectQuery query8 = new ObjectQuery(
"SELECT * FROM Win32_SystemDriver");
ManagementObjectSearcher searcher8 =
new ManagementObjectSearcher(scope, query8);
foreach (ManagementObject queryObj in searcher8.Get())
{
string[] arrTeamMembers = new string[] { queryObj["Description"].ToString() };
foreach (var item in arrTeamMembers)
{
richTextBox1.Text = item;
}
}
Do you have any ideia how can I display all the info listing into the RichTextBox?
Try following :
List<string> arrTeamMembers = new List<string>();
foreach (ManagementObject queryObj in searcher8.Get())
{
arrTeamMembers.Add(queryObj["Description"].ToString());
}
richTextBox1.Text = string.Join(",", arrTeamMembers);
Your approach is ok. There is one thing which you missed: richTextBox1.Text is a so-called property that stores a string. Now the reason why it shows you only the last driver is that you set this property to a new value for each driver you have in your array. So for the first driver it sets it to "driverA" and for the second to "driverB". What you are looking for is the += operator --> richTextBox1.Text += item;. If you want to add spaces between the drivers you can do something like richTextBox1.Text += $" {item}";. This way you have a leading whitespace but formating is personal preference.
Please note that ManagementObjectSearcher are IDisposable and therefore should be disposed.
ObjectQuery query8 = new ObjectQuery("SELECT * FROM Win32_SystemDriver");
using (ManagementObjectSearcher searcher8 = new ManagementObjectSearcher(scope, query8))
{
List<string> arrTeamMembers = new List<string>();
foreach (ManagementObject queryObj in searcher8.Get())
{
arrTeamMembers.Add(queryObj["Description"].ToString());
}
richTextBox1.Text = string.Join(Environment.NewLine, arrTeamMembers);
}
var query8 = new ObjectQuery("SELECT * FROM Win32_SystemDriver");
var searcher8 = new ManagementObjectSearcher(scope, query8);
var strbuilder = new StringBuilder();
foreach (var queryObj in searcher8.Get())
strbuilder.AppendLine($"{queryObj["Description"].ToString()}");
richTextBox1.Text = strbuilder.ToString();

Using ManagementObjectSearcher to query Win32_PnPEntity comes back empty

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;
}

How to query GetMonitorBrightness from C#

How does GetMonitorBrightness http://msdn.microsoft.com/en-us/library/ms775205.aspx work? Can someone give me an actual working implementation calling this code in C#?
I'm trying to retrieve the allowed brightness levels my laptop supports.
I have the following working code that sets the brightness from 1 to ~150. But I'm looking for the allowed input values (min max values).
static void SetBrightness(byte targetBrightness)
{
ManagementScope scope = new ManagementScope("root\\WMI");
SelectQuery query = new SelectQuery("WmiMonitorBrightnessMethods");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
using (ManagementObjectCollection objectCollection = searcher.Get())
{
foreach (ManagementObject mObj in objectCollection)
{
mObj.InvokeMethod("WmiSetBrightness",
new Object[] { UInt32.MaxValue, targetBrightness });
break;
}
}
}
}
While using Interop should be possible this function is also available through WMI. Changing my original code a bit resulted in the following code that should work:
ManagementScope scope;
SelectQuery query;
scope = new ManagementScope("root\\WMI");
query = new SelectQuery("SELECT * FROM WmiMonitorBrightness");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
using (ManagementObjectCollection objectCollection = searcher.Get())
{
foreach (ManagementObject mObj in objectCollection)
{
Console.WriteLine(mObj.ClassPath);
foreach (var item in mObj.Properties)
{
Console.WriteLine(item.Name + " " +item.Value.ToString());
if(item.Name =="CurrentBrightness")
//Do something with CurrentBrightness
}
}
}
}
Now I'm really curious how to handle 'special' cases like non laptop Screen's and if they implement any way to influence brightness.
The function has an output of the minimum and maximum values:
LPDWORD pdwMinimumBrightness=NULL;
LPDWORD pdwMaximumBrightness=NULL;
HANDLE pmh = pPhysicalMonitors[0].hPhysicalMonitor;
GetMonitorBrightness(pmh, pdwMinimumBrightness, pdwMaximumBrightness);
This is assuming that you want the values for the first monitor.

Querying the size of a drive on a remote server is giving me a not found error

Here's what I have:
public static bool DriveHasLessThanTenPercentFreeSpace(string server)
{
long driveSize = 0;
long freeSpace = 0;
var oConn = new ConnectionOptions {Username = "username", Password = Settings.Default.SQLServerAdminPassword};
var scope = new ManagementScope("\\\\" + server + "\\root\\CIMV2", oConn);
scope.Connect();
var query = new ObjectQuery("SELECT FreeSpace FROM Win32_LogicalDisk where DeviceID = 'D:'");
var searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
//the FreeSpace value is in bytes
freeSpace = Convert.ToInt64(m["FreeSpace"]);
//error happens here!
driveSize = Convert.ToInt64(m["Size"]);
}
long percentFree = ((freeSpace / driveSize) * 100);
if (percentFree < 10)
{
return true;
}
return false;
}
This line of code is giving me an error:
driveSize = Convert.ToInt64(m["Size"]);
The error says:
ManagementException was unhandled by user code
Not found
I'm assuming the query to get the drive size is wrong.
Please note that I AM getting the freeSpace value at the line:
freeSpace = Convert.ToInt64(m["FreeSpace"]);
So I know the query IS working for freeSpace.
Can anyone give me a hand?
It needs to be SELECT * FROM Win32_LogicalDisk... in your query.
Because you are selecting "FreeSpace" in your query nothing else but the free space will be returned, and everything else will throw an exception.
In the case you dont want to return everything (because its a remote query) you have a couple options:
I'm not sure if management will allow you to select only two items? I'm not familiar with the syntax. However you could try SELECT FreeSpace, Size FROM Win32_LogicalDisk...
If not, you could simply do two queries, one for the freespace, and one for the size.

WMI turn off disks

I want to turn off disks (WMI). So far, I have the following code:
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
ObjectQuery query = new ObjectQuery("SELECT * FROM CIM_DiskDrive");
//create object searcher
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
//enumerate the collection.
foreach (ManagementObject m in queryCollection)
{
Console.WriteLine("in set power state for: " + m.Path);
ManagementOperationObserver obs = new ManagementOperationObserver();
obs.Progress += new ProgressEventHandler(obs_Progress);
obs.Completed += new CompletedEventHandler(obs_Completed);
m.InvokeMethod(obs, "SetPowerState", new object[]{"7"});
}
however, disk activity keeps on happening. Any ideas on what is going on will be appreciated.
SetPowerState is not implemented by WMI:
http://msdn.microsoft.com/en-us/library/aa387254(v=VS.85).aspx
checking CompletedEventArgs.Status will also return MethodNotImplemented telling us that this is the case. If you want to use that method you must implement your own provider.

Categories

Resources