判断系统是否安装声卡[C#]
判断当前机器是否安装声卡(C#)
背景:
我们在开发某些系统时,有时需要检测当前机器上是否安装了声卡.
解决办法:
我们在net framework(C#)环境下可以通过调用winmm.dll库文件中的 waveOutGetNumDevs 和 midiOutGetNumDevs() 函数.
waveOutGetNumDevs()的返回值表示当前机器上波形输出设备个数
midiOutGetNumDevs()的返回值表示当前机器上MIDI输出设备个数
示例代码:
1.引入命名空间:
using System.Runtime.InteropServices;
2.引入winmm.dll库文件
[ DllImport( "Winmm.dll", CharSet=CharSet.Auto )]
public static extern int waveOutGetNumDevs();
[ DllImport( "Winmm.dll", CharSet=CharSet.Auto )]
public static extern int midiOutGetNumDevs();
private void btnBegin_Click(object sender, System.EventArgs e)
{
try
{
if(waveOutGetNumDevs() > 0 && midiOutGetNumDevs() >0 )
{
MessageBox.Show("系统检测到您当前机器已安装声卡!");
}
else
{
MessageBox.Show("系统检测到您当前机器未安装声卡!");
}
}
catch(Exception ex)
{
MessageBox.Show("检测当前机器是否安装声卡失败: "+ex.Message);
}
}