Enumerate audio input devices with WMI - c#

I am using NAudio in my C# project, and I am looking for a way to enumerate audio input devices (microphone etc.), so i can get full name of them (not only the 31-characters long name that i can get from NAudio). I went through a few posts where people were enumerating audio output devices with WMI:
ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_SoundDevice");
ManagementObjectCollection objCollection = objSearcher.Get();
Is it possible to enumerate input devices this way as well?
Thanks

To explore the WMI queries you can use a tool that generates the WMI code for you. You'll have plenty of WMI management classes to get the information from.
You can download the tool from Microsoft download center here
I wrote a blog post few years back about using WMI management services for administration. Hope this would give you a head start.
Here's snippet generated from the tool to get the list of installed sound cards on the device.
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_SoundDevice");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("List of sound cards installed");
Console.WriteLine("-----------------------------------");
Console.WriteLine("ProductName: {0}", queryObj["ProductName"]);
Console.WriteLine("Availability: {0}", queryObj["Availability"]);
Console.WriteLine("Caption: {0}", queryObj["Caption"]);
Console.WriteLine("ConfigManagerErrorCode: {0}", queryObj["ConfigManagerErrorCode"]);
Console.WriteLine("ConfigManagerUserConfig: {0}", queryObj["ConfigManagerUserConfig"]);
Console.WriteLine("CreationClassName: {0}", queryObj["CreationClassName"]);
Console.WriteLine("Description: {0}", queryObj["Description"]);
Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
Console.WriteLine("DMABufferSize: {0}", queryObj["DMABufferSize"]);
Console.WriteLine("ErrorCleared: {0}", queryObj["ErrorCleared"]);
Console.WriteLine("ErrorDescription: {0}", queryObj["ErrorDescription"]);
Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);
Console.WriteLine("LastErrorCode: {0}", queryObj["LastErrorCode"]);
Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
Console.WriteLine("MPU401Address: {0}", queryObj["MPU401Address"]);
Console.WriteLine("Name: {0}", queryObj["Name"]);
Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);
Console.WriteLine("PowerManagementSupported: {0}", queryObj["PowerManagementSupported"]);
Console.WriteLine("Status: {0}", queryObj["Status"]);
Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);
Console.WriteLine("SystemCreationClassName: {0}", queryObj["SystemCreationClassName"]);
Console.WriteLine("SystemName: {0}", queryObj["SystemName"]);
}
}
catch (ManagementException e)
{
Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
}
}
Here's the output -
-----------------------------------
List of sound cards installed
-----------------------------------
ProductName: Realtek High Definition Audio
Availability:
Caption: Realtek High Definition Audio
ConfigManagerErrorCode: 0
ConfigManagerUserConfig: False
CreationClassName: Win32_SoundDevice
Description: Realtek High Definition Audio
DeviceID: HDAUDIO\FUNC_01&VEN_10EC&DEV_0662&SUBSYS_103C304A&REV_1001\4&3867FD9A&0&0001
DMABufferSize:
ErrorCleared:
ErrorDescription:
InstallDate:
LastErrorCode:
Manufacturer: Realtek
MPU401Address:
Name: Realtek High Definition Audio
PNPDeviceID: HDAUDIO\FUNC_01&VEN_10EC&DEV_0662&SUBSYS_103C304A&REV_1001\4&3867FD9A&0&0001
PowerManagementSupported: False
Status: OK
StatusInfo: 3
SystemCreationClassName: Win32_ComputerSystem
SystemName: PC-2322Q1

These are sound devices, so it includes input and output devices. Soundcards can have 0 or more outputs and 0 or more inputs.

Related

C# how to effectively detect which graphic card is used [duplicate]

My C# application sits on the embedded box which has Intel motherboard and graphics chipset. ATI graphics card is put on to PCI express. Generally graphics card drives the video, but if ATI card fails then the video comes out from graphics chipset.
I have to detect the failure of ATI graphics card for diagnostic purposes.
Any ideas/sample code on how to do this.
Thanks in advance
Raju
This should hopefully get you started.
Add a reference to System.Management, then you can do this:
ManagementObjectSearcher searcher
= new ManagementObjectSearcher("SELECT * FROM Win32_DisplayConfiguration");
string graphicsCard = string.Empty;
foreach (ManagementObject mo in searcher.Get())
{
foreach (PropertyData property in mo.Properties)
{
if (property.Name == "Description")
{
graphicsCard = property.Value.ToString();
}
}
}
In my case, graphicsCard is equal to
NVIDIA GeForce 8400 GS (Microsoft
Corporation - WDDM v1.1)
I'm not a fan of how the selected answer only returns the first video controller. Also, there's no need to loop over all the properties. Just get the ones you need. If CurrentBitsPerPixel is not null, then you're looking at one of the active controllers. I'm using Win32_VideoController as suggested by #bairog, instead of the deprecated Win32_DisplayConfiguration.
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
foreach (ManagementObject mo in searcher.Get())
{
PropertyData currentBitsPerPixel = mo.Properties["CurrentBitsPerPixel"];
PropertyData description = mo.Properties["Description"];
if (currentBitsPerPixel != null && description != null)
{
if (currentBitsPerPixel.Value != null)
System.Console.WriteLine(description.Value);
}
}
My machine has 3 video controllers. The first one is not active (ShoreTel). The second one is active, but is not the video card (Desktop Authority). The third one is my NVidia. This code will print out both the DA controller and the NVidia controller.
Promoted answer works only for single video card system. When I have ATI and Nvidia cards - WMI query returns ATI even if that monitor is plugged into Nvidia card, dxdiag shows Nvidia and games runs on that card (usage).
The only way I could determine right video card was using SlimDX to create DX device and examine what card it used. However that .dll weights over 3Mb.
var graphicsCardName = new Direct3D().Adapters[0].Details.Description;
Your question isn't entirely clear, so I'm not sure if the follwing idea will help or not.
Perhaps something very simple would suffice:
If the two graphics cards run different resolutions check the monitor resolution using:
System.Windows.Forms.SystemInformation.PrimaryMonitorSize
Similarly, if one card supports more than one monitor, check the number of monitors using SystemInformation.MonitorCount.
I tried all the approaches in this question but none gives me a correct answer. However I found it possible to get your current using the Win32_DisplayControllerConfiguration class. Although according to MSDN this class is obsolete, it's the only one returning a correct answer:
using System;
using System.Management;
using System.Windows.Forms;
namespace WMISample
{
public class MyWMIQuery
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_DisplayControllerConfiguration");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("----------------------------------- ");
Console.WriteLine("Win32_DisplayControllerConfiguration instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("Name: {0}", queryObj["Name"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
}
}
}
(Code generated by WMI Code Creator, a great tool if you are messing with WMI.)
This gives GeForce GTX 1080 on my Windows 10 (RS2) + Intel(R) HD Graphics 4600 + NVIDIA GeForce GTX 1080 system.
Sometimes I need to switch between the Nvidia GPU and onboard GPU. To know which is connected to the monitor, I use the property MinRefreshRate. It works reliably for me, not CurrentBitsPerPixel.
public static void UpdateActiveGpu()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
foreach (ManagementObject mo in searcher.Get())
{
PropertyData minRefreshRate = mo.Properties["MinRefreshRate"];
PropertyData description = mo.Properties["Description"];
if (minRefreshRate != null && description != null && minRefreshRate.Value != null)
{
Global.Instance.activeGpu = description.Value.ToString();
break;
}
}
}

Is it possible to detect Gender using Google.Cloud.Vision

I found some code examples of detecting face:
var image = Image.FromFile(filePath);
var client = ImageAnnotatorClient.Create();
var response = client.DetectFaces(image);
int count = 1;
foreach (var faceAnnotation in response)
{
Console.WriteLine("Face {0}:", count++);
Console.WriteLine(" Joy: {0}", faceAnnotation.JoyLikelihood);
Console.WriteLine(" Anger: {0}", faceAnnotation.AngerLikelihood);
Console.WriteLine(" Sorrow: {0}", faceAnnotation.SorrowLikelihood);
Console.WriteLine(" Surprise: {0}", faceAnnotation.SurpriseLikelihood);
}
But I couldn't find 'IsMale' or 'Gender' property in faceAnnotation. How can I detect Gender using Google.Cloud.Vision?
I think AWS Rekognition could provide gender:
Q: What face attributes can I get from Amazon Rekognition?
Amazon Rekognition returns the following facial attributes for each face detected, along with a bounding box and confidence score for each attribute:
Gender
Smile
...
see: https://aws.amazon.com/rekognition/faqs/

Read property from device manager?

There is some way to read property from device manager?
I need read the battery status of surface dial without pair to the surface dial from my desktop C# WPF app.
Link to screen capture here
You can play arround with WMI queries. Here is a little example:
static void Main(string[] args)
{
string query = #"SELECT * FROM Win32_PnPEntity";
var moSearch = new ManagementObjectSearcher(query);
var moCollection = moSearch.Get();
foreach (ManagementObject mo in moCollection)
{
Console.WriteLine(mo.Path.ToString());
foreach (var item in mo.Properties)
{
Console.WriteLine($"{item.Name}: {item.Value}");
}
Console.WriteLine();
}
Console.ReadKey();
}
Remember to add System.Management reference to the project.
Look on the Device Manager for the exact DeviceId or DevicePath in order to put a WHERE statement in your query.
For more info about classes and queries in WMI here is the official documentation https://learn.microsoft.com/en-us/windows/desktop/cimwin32prov/computer-system-hardware-classes
This is the result running your example, no idea how query for "Bluetooth LE battery level"
\\DESKTOP-54P9Q3C\root\cimv2:Win32_PnPEntity.DeviceID="BTHLE\\DEV_BC83851FE704\\7&222CF7DF&0&BC83851FE704"
Availability:
Caption: Surface Dial
ClassGuid: {e0cbf06c-cd8b-4647-bb8a-263b43f0f974}
CompatibleID: System.String[]
ConfigManagerErrorCode: 0
ConfigManagerUserConfig: False
CreationClassName: Win32_PnPEntity
Description: Bluetooth LE Device
DeviceID: BTHLE\DEV_BC83851FE704\7&222CF7DF&0&BC83851FE704
ErrorCleared:
ErrorDescription:
HardwareID: System.String[]
InstallDate:
LastErrorCode:
Manufacturer: Microsoft
Name: Surface Dial
PNPClass: Bluetooth
PNPDeviceID: BTHLE\DEV_BC83851FE704\7&222CF7DF&0&BC83851FE704
PowerManagementCapabilities:
PowerManagementSupported:
Present: True
Service: BthLEEnum
Status: OK
StatusInfo:
SystemCreationClassName: Win32_ComputerSystem
SystemName: DESKTOP-54P9Q3C

Display General tab information of property window of local disks in my computer

I need to access the type of disk and display it in the C# code. In this image "
Local Disk Image" the type is local disk.
In this image "Cluster Disk Image" the type of disk is cluster disk.
I have used driveInfo class to get the drive details. but I'm not able to display whether the drive is cluster or local disk. d.DriveType returns whether it is a removable or fixed types. and not the required local disk or cluster type.
My code is added below:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" Drive type: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
Console.WriteLine(" File system: {0}", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes",
d.AvailableFreeSpace);
Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace);
Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
}
Is there any way to display the required info in C# code?
Thank you.

Getting USB drive name attached to windows ce device

How to get name of usb drive attahced to my Windows ce device using c# or any or the native API.
You may be able to start with the C# DriveInfo class:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
Console.WriteLine(" File system: {0}", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes",
d.AvailableFreeSpace);
Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace);
Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
}
http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx
or this may help:
Get List of connected USB Devices
The folder name is localized and can change from device to device. The way I do it is to query it from the same registry location the driver used for getting the name:
using(var key = Registry.LocalMachine.OpenSubKey(
#"\System\StorageManager\Profiles\USBHDProfile"))
{
USBDiskFolderName = key.GetValue("Folder").ToString();
}

Categories

Resources