How to identify monitor or projector connect to pc? - c#

I am writing a small C# application to identify which type of display connect to pc
A Monitor
A TV screen
A projector
I try to do with
Screen.AllScreens
EnumDisplayDevices()
but it does not help anything.
When I use
var mbs = new ManagementObjectSearcher("Select * From CIM_LogicalDevice");
ManagementObjectCollection mbsList = mbs.Get();
I can get device id:
DISPLAY\OTMFB0E\4&9C24ACE&0&UID16843008
DISPLAY\DELD058\4&9C24ACE&0&UID50725632
But there is not any value which is used to identify between a normal monitor and a projector.
Is there any way to do?
Or which information will be different of these display types?

Related

Where do I find the name displayed in "Devices and Printers"

I have a serial device connected via Bluetooth. It shows up nicely on COM4. I can communicate with it without a problem.
I want to make it simpler for the user to locate (ideally, I'll auto-detect it), so I want to find it by name. In the "Devices and Printers" list, I get a valid name, which is perfect. However, I can't seem to find that value programatically. I've tried a ton of stuff using the "ManagementObjectSearcher" class, including listing out all the Properties and SystemProperties, but no values match the name displayed in "Devices and Printers".
If I look in the "Device Manager" list, it just shows "Standard Serial over Bluetooth link (COM4)", which is not useful for identifying it, obviously.
So how the heck to I get the displayed name in the "Devices and Printers" list?
is this what you're looking for?
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printersettings.installedprinters(v=vs.110).aspx
So, I found a solution. I grabbed the library from these guys:
http://32feet.codeplex.com/
Using that library, added these 2 lines:
BluetoothClient client = new BluetoothClient();
BluetoothDeviceInfo[] devices = client.DiscoverDevices();
That gave me the device "DeviceName" (the name I was after) and "DeviceAddress" (a chunk of the device id, basically).
I then queried the system using the "ManagementObjectSearcher", which gave me a list of COM ports and device IDs (System.Management namespace).
ConnectionOptions options = ProcessConnection.ProcessConnectionOptions();
ManagementScope connectionScope = ProcessConnection.ConnectionScope(Environment.MachineName, options, #"\root\CIMV2");
ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity");
ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery);
...etc as I looped over the results, pulled out the COM ports, and so on
I mapped the device IDs from the "ManagementObject" values back to the "devices" list, merged the results, and ended up with something that had the name, device id, a flag indicating if it was a bluetooth device, and the "human readable" name from the bluetooth device, if it existed.
Painful, but it works fairly well. It's slow (client.DiscoverDevices() takes awhile), but that's survivable in my case.

List all USB audio headsets connected to a Windows PC

I want to use C# to retrieve the USB headset devices connected to PC. I tried the below solutions but didn't work:
Solution 1:
How to enumerate audio out devices in c#
I tried this but the device name appears as "(Generic USB Audio)" and not the actual name.
Solution 2:
How to get the default audio device?
Solution 3:
Detecting Audio Input & output devices connected to system
Solution 2 and Solution 3 gave me the below result:
The device name is truncated to 31 characters.
Eg: "Microphone (Sennheiser VOICE 689"
****Question: Is there any way I can get the complete name of the device?****
If you know it's an USB audio device, and assuming the driver is correctly written for the device, you could do:
foreach (ManagementObject drive in
new ManagementObjectSearcher(
"select Name from Win32_USBDevice where Service='usbaudio'").Get())
{
{
string s = drive["Name"].ToString();
// Continue
}
}
Addition
You're only getting 31 characters (technically 32) because the PInvoke to the native .DLLs use a char[32], so it can't return more than that; you won't get what you need from solution 1 & 2.
Also, I don't know why you can't use Win32_USBDevice, as I'm also using Win7 x64 and I'm having no problems. This link might help you.
Possible Alternate
You might be able to use the Win32_PnPEntity class:
foreach (ManagementObject drive in
new ManagementObjectSearcher(
"select Name from Win32_PnPEntity where Service='usbaudio'").Get())
{
{
string s = drive["Name"].ToString();
// Continue. Can look at Description, Caption, etc. too
}
}

How to get list of windows (DirectSound? ASIO?) audio devices?

1. I need list of all device names and types (playback or recording). How can I get them?
2. I also need to determine if device is Playback or Recording device.
By device names I mean names visible here under Playback and Recording tab (screenshot below). Im not familiar with audio under windows, i don't know if these devices are ASIO, DirectSound or something else.
My application should be comaptybile with Windows Vista/7/8, so I decided to use .NET 3.5, but I can use any .NET version supported by Windows Vista/7/8.
Edit: I tried to get these from WMI "SELECT * FROM Win32_SoundDevice", but this is not what I mean. It returns hardware devices, not devices visible in windows sound configuration.
Use How to enumerate audio out devices in c#:
ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_SoundDevice");
ManagementObjectCollection objCollection = objSearcher.Get();
foreach (ManagementObject obj in objCollection)
{
foreach (PropertyData property in obj.Properties)
{
Console.Out.WriteLine(String.Format("{0}:{1}", property.Name, property.Value));
}
}

Detect number of display monitor

I already tried Screen.AllScreen, SystemInformation.MonitorCount, and WMI but all of them failed.
My application runs as a windows service, hence no visual Form or UI. Both Screen.AllScreen and SystemInformation.MonitorCount returns 1 even when I have 2 monitors. If I run my application in console, it returns the correct count of display but my requirement is that my application to run as a windows service (no UI).
Thanks!
Found answer to my own question.
Still end up using WMI.
I was initially using Win32_DesktopMonitor giving a non-reliable answer.
Using this query:
"SELECT * FROM Win32_PnPEntity WHERE Service = 'monitor'"
WMI returns the correct monitor instance connected to my PC.
I went with Win32_PnPEntity because it represents the information for Plug and Play Devices in your Device Manager, which will show you when you have a monitors plugged in. The way the query works in "searcher" is more likely to be accurate than others since it uses the Like Operator. I do this because on 3 different computers the monitor entries in Device manager appeared differently. E.g. (Pnp-Monitor, Pnp Monitor (Standard), Generic Pnp Monitor).
private int CountMonitorsInstalled()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "select * from Win32_PnPEntity WHERE Name LIKE '%PnP%Monitor%'");
return searcher.Get().Count;
}
catch(Exception ex)
{
return 0;
}
}

Get list of audio devices and select one using c#

Hi I am creating a desktop based application in windows using C#.
I have to show list of all available audio & video devices in 2 different combo boxes.
Selecting any device from combo box will set that particular device as the default one
I am using WMI.
Code to get list of available audio devices:
ManagementObjectSearcher mo =
new ManagementObjectSearcher("select * from Win32_SoundDevice");
foreach (ManagementObject soundDevice in mo.Get())
{
String deviceId = soundDevice.GetPropertyValue("DeviceId").ToString();
String name = soundDevice.GetPropertyValue("Name").ToString();
//saving the name and device id in array
}
if i try to set the device like this:
using (RegistryKey audioDeviceKey =
Registry.LocalMachine.OpenSubKey(audioDevicesReg
+ #"\" + audioDeviceList.SelectedText.ToString(), true)){}
i get exception :
System.Security.SecurityException occurred in mscorlib.dll
Now I have few questions:
1) How to set the selected device as the default audio device?
2) The array contains device name as : "High Definition audio device"
even when I have attached a headset.
3) I want the list as speaker,headset etc...How to get that?
can anybody point me in the right direction?
There is no documented mechanism for changing the default audio device.
That's because you're enumerating the physical audio devices, not the audio endpoints.
You want to use the IMMDeviceEnumerator API to enumerate the audio endpoints (speakers, etc).
Unfortunately there is no managed interop published by Microsoft for the IMMDeviceEnumerator API, you'll need to define your own (there are several definitions available on the internet).
I am answering too late to this question.. but it may be helpful for others.
Lync 2013 SDK provides DeviceManager class which list all the audio and video devices in collections
LyncClient.GetClient().DeviceManager.AudioDevices enumerates all the audio devices on the system
LyncClient.GetClient().DeviceManager.VideoDevices enumerates all the video devices on the system
So, one can set the device as:
LyncClient client = LyncClient.GetClient();
DeviceManager dm = client.DeviceManager;
dm.ActiveAudioDevice = (AudioDevice)dm.AudioDevices[0]; //or any other found after foreach
dm.ActiveVideoDevice = (VideoDevice)dm.VideoDevices[0]; //or any other found after foreach
HTH.

Categories

Resources