c# - 如何在 Windows 10 和 > .net 4.5 中用 c# 枚举音频输入设备?

标签 c# .net windows winforms

如何获取 Windows 系统上已安装音频输入设备的列表? 操作系统:Windows(10) 框架:.Net >= 4.5 语言:c#

我已经尝试过使用这个:

 ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
   "SELECT * FROM Win32_SoundDevice");

 ManagementObjectCollection objCollection = objSearcher.Get();

但这给了我所有设备的列表,我只需要输入设备列表

最佳答案

您可以使用 DirectSoundCaptureEnumerate :

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class DirectSoundDevices
{
    [DllImport("dsound.dll", CharSet = CharSet.Ansi)]
    static extern void DirectSoundCaptureEnumerate(DSEnumCallback callback, IntPtr context);
    delegate bool DSEnumCallback([MarshalAs(UnmanagedType.LPStruct)] Guid guid,
        string description, string module, IntPtr lpContext);
    static bool EnumCallback(Guid guid, string description, string module, IntPtr context)
    {
        if (guid != Guid.Empty)
            captureDevices.Add(new DirectSoundDeviceInfo(guid, description, module));
        return true;
    }
    private static List<DirectSoundDeviceInfo> captureDevices;
    public static IEnumerable<DirectSoundDeviceInfo> GetCaptureDevices()
    {
        captureDevices = new List<DirectSoundDeviceInfo>();
        DirectSoundCaptureEnumerate(new DSEnumCallback(EnumCallback), IntPtr.Zero);
        return captureDevices;
    }
}
public class DirectSoundDeviceInfo
{
    public DirectSoundDeviceInfo(Guid guid, string description, string module)
    { Guid = guid; Description = description; Module = module; }
    public Guid Guid { get; }
    public string Description { get; }
    public string Module { get; }
}

您还可以使用 waveInGetDevCaps但对于设备名称,它返回设备名称的前 32 个字符。

关于c# - 如何在 Windows 10 和 > .net 4.5 中用 c# 枚举音频输入设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57519046/

相关文章:

c++ - 结合批处理和 C++

C# : Generic factory object instantiation by passing type (type retrieved from another static object)

c# - 带有代码的 ASP.NET 完整网站示例

c# - 在 PictureBox 上画线

Windows 10 Jenkins 管道无法连接到存储库

Windows CloudFormation 脚本、元数据命令未运行

c# - 将字符串数组的元素链接到十进制值

c# - 使用 Rhino.Mocks 检查传递给委托(delegate)的预期值

c# - 将 .asmx Web 服务作为 Windows 服务运行

.net - WCF:服务器端错误处理。最佳实践