2

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"

enter image description here

****Question: Is there any way I can get the complete name of the device?****

Community
  • 1
  • 1
tom
  • 75
  • 1
  • 10
  • Are you shure that Solution 1 gives you only one device. I tested it on my machine and it returns 2 devices. First one is "(Generic USB Audio)" and second looks like "real one" I have – Nick Jun 03 '14 at 20:15
  • It gives multiple devices. For headset it appears like this "(Generic USB Audio)" – tom Jun 03 '14 at 21:18
  • @JohnSaunders: You're doing it badly, though. Convert tag lists into real phrases using the tags, please. http://meta.stackoverflow.com/q/251859/103167 – Ben Voigt Jun 03 '14 at 22:33
  • @BenVoigt: sorry, disagree in this case. The vast majority of any answers have nothing to do with C#, and the tag adequately informs those who answer that they should answer in C#. – John Saunders Jun 03 '14 at 22:50
  • @John:there's no C# in the title. Was briefly while I thought about which words would really describe the problem accurately. – Ben Voigt Jun 03 '14 at 22:52
  • @BenVoigt: that's kind of my point. I took a "C#" out of the title, where it represented "metadata" (tags), not "data" (title). – John Saunders Jun 03 '14 at 23:12
  • @JohnSaunders: Ahh, sorry. I saw other, more useful content removed from the title, and didn't realize that was the OP changing it in response to your comment, and not your change. – Ben Voigt Jun 03 '14 at 23:17
  • By "complete name" do you mean the "friendly name" of the audio endpoint? See http://stackoverflow.com/a/24220881/67824 – Ohad Schneider Jun 14 '14 at 14:24

1 Answers1

0

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
    }
}
Community
  • 1
  • 1
Kcvin
  • 5,073
  • 2
  • 32
  • 54
  • (ManagementObject drive in new ManagementObjectSearcher( "select Name from Win32_USBHub").Get()) { { string s = drive["Name"].ToString(); } } This is gave me "Generic USB Hub" and not the actual name – tom Jun 04 '14 at 16:03
  • I am using windows 7. Solution 2 and 3 gave me the actual name but the string was truncated to 31 characters. – tom Jun 04 '14 at 16:06
  • I could not resolve the Win32_USBDevice issue. I tried the solution provided in the link but it didn't work. – tom Jun 04 '14 at 20:52
  • I tried that as well but didn't work. I installed WMI code creator and tried few classes but nothing worked. – tom Jun 05 '14 at 02:41
  • Have you tried it on other machines because that shouldn't be happening. – Kcvin Jun 05 '14 at 03:04
  • I will check tomorrow. – tom Jun 06 '14 at 05:05