Trying to access battery level, c# - c#

I'm trying to create an app for windows 8 using c# to display my current battery level. I'm trying to query the win32_battery class for its relevant properties,but I'm getting an unusual result. Here's my code:
private void btn1_Click(object sender, EventArgs e)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Battery");
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject obj in collection)
{
txtBox.AppendText(obj.ToString() + "\r\n");
};
}
My only result in the txtBox is
\\MIKESLAPTOP\root\cimv2:Win32_Battery.DeviceID=" ASUSTeKX401-44"
Any ideas why I am only reading theDevideID property? All guidance is greatly appreciated.

This is the expected output. You forgot to enumerate the properties of the query. Make it look similar to this:
foreach (ManagementObject obj in searcher.Get()) {
foreach (var prop in obj.Properties) {
if (prop.Value != null) {
txtBox.AppendText(string.Format("{0} = {1}", prop.Name, prop.Value));
}
}
}

Related

Getting MSFT_Partition from MSFT_Disk using Associators

I am trying to get a list of MSFT_Partitions from a MSFT_Disk object and loop over them.
This is the code I have been using so far but it always outputs an System.Management.ManagementException: 'Invalid query ' exception.
This is the code I am using at the moment:
public static void GetDiskInfo() {
var rawDiskInfos = new ManagementObjectSearcher("root\\Microsoft\\Windows\\Storage", "SELECT * FROM MSFT_Disk");
foreach(var rawDiskInfo in rawDiskInfos.Get()) {
Console.WriteLine(rawDiskInfo["FriendlyName"]);
GetPartitionInfo(rawDiskInfo["ObjectId"]);
}
}
public static void GetPartitionInfo(object objectId) {
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\Microsoft\\Windows\\Storage");
var query = new ObjectQuery("ASSOCIATORS OF {MSFT_Disk.ObjectId=\"" + objectId + "\"} WHERE AssocClass = MSFT_DiskToPartition");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach(var partiton in queryCollection) {
Console.WriteLine(partiton["Guid"]);
}
}
I already tried a lot of solutions I found online and all of them resulted in the same exception.
Thank you very much for your help!
Most queries you'll find on the internet don't do it, but in the general case, you must escape raw strings you pass to WQL queries (in your case objectId contains special characters), using the backslash character, with a method like this:
public static string EscapeWql(string text)
{
if (text == null)
return null;
var sb = new StringBuilder(text.Length);
foreach (var c in text)
{
if (c == '\\' || c == '\'' || c == '"')
{
sb.Append('\\');
}
sb.Append(c);
}
return sb.ToString();
}
So your method should now look like this:
public static void GetPartitionInfo(object objectId)
{
var scope = new ManagementScope(#"root\Microsoft\Windows\Storage");
var query = new ObjectQuery("ASSOCIATORS OF {MSFT_Disk.ObjectId=\"" + EscapeWql((string)objectId) + "\"} WHERE AssocClass = MSFT_DiskToPartition");
using (var searcher = new ManagementObjectSearcher(scope, query))
{
using (var queryCollection = searcher.Get())
{
foreach (var partition in queryCollection)
{
Console.WriteLine(partition["Guid"]);
}
}
}
}
PS: don't forget using statements on IDisposable classes.

C# WMI How to get name of current audiodevice

ManagementObjectSearcher mo = new ManagementObjectSearcher("select * from Win32_SoundDevice");
foreach (ManagementObject soundDevice in mo.Get())
{
Console.WriteLine(soundDevice.GetPropertyValue("DeviceId"));
Console.WriteLine(soundDevice.GetPropertyValue("Name"));
}
With this I can get names of all audiodevices. But how to know which is used right now?
You could use NAudio
And then something like this oneliner:
string curDevName = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia).FriendlyName;

populate combobox with WMI query using C#

Can someone help me with this problem.I'm trying to populate combobox with the value from WMI query. But I don't know how to do it. Googled to find the solution but to no avail.
private void Form_Load1(object sender, EventArgs e)
{
ManagementScope oScope = new ManagementScope("\\root\\cimv2");
ObjectQuery oQuery = new ObjectQuery("select DisplayName from Win32_Service");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oScope, oQuery);
ManagementObjectCollection oCol = oSearcher.Get();
foreach (ManagementObject col in oCol)
{
String displayName = col["DisplayName"].ToString();
comboBox1.Items.Add(displayName);
}
I change to foreach loop but it displays all the properties value. How to choose 'DisplayName' instead? Thanks

Visual Studio 2010 SP1 breaks things?

I'm using this little code snippet to catch Java processes with certain parameters:
string query = "Select * From Win32_Process Where Name = 'javaw.exe'";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();
foreach (ManagementObject obj in processList)
{
string cmdLine = obj.GetPropertyValue("CommandLine").ToString();
if (cmdLine.IndexOf("someapplication") != -1)
{
// ...
}
}
This code worked like a charm just a couple of days ago when I didn't have SP1 for VS2010. Now it throws a null pointer exception on line 7. I'm trying to compile for .NET Framework 2.0.
Help!? :/
if (cmdLine != null && cmdLine.IndexOf("someapplication") != -1)
It probably has less to do with SP1 and more to do with a Java update. Just check for null:
string query = "Select * From Win32_Process Where Name = 'javaw.exe'";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();
foreach (ManagementObject obj in processList)
{
object cmdLineValue = obj.GetPropertyValue("CommandLine");
if(cmdLineValue != null) {
string cmdLine = cmdLineValue.ToString();
if (cmdLine.IndexOf("someapplication") != -1)
{
// ...
}
}
}

How I can get All Network Interfaces(including that are not running)?

I'm using this code:
NetworkInformation.NetworkInterface[] interfaces = NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
the above code retrieving only the active network connections, I need of all. How I do this?
Thanks in advance. :)
using System.Management;
ManagementObjectSearcher query = new ManagementObjectSearcher(
"SELECT * FROM Win32_NetworkAdapterConfiguration" );
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
Console.WriteLine(mo["Description"].ToString());
}
Edit:
to find all others ["Properties"] name, change the foreach like this:
foreach (ManagementObject mo in queryCollection)
{
foreach (PropertyData pd in mo.Properties)
{
Console.WriteLine(pd.Name);
}
}

Categories