【发布时间】:2018-08-10 14:09:47
【问题描述】:
我有一个用 c# 编写的 WPF 应用程序,它使用带有 naudio 库的麦克风设备,在 Windows 10 更新版本 1803 上添加了在麦克风处访问的隐私设置。
如果用户有允许隐私标志,我的应用程序可以正常工作,否则我的应用程序无法正常工作。那么如何通过c#查看这个隐私设置呢?
【问题讨论】:
我有一个用 c# 编写的 WPF 应用程序,它使用带有 naudio 库的麦克风设备,在 Windows 10 更新版本 1803 上添加了在麦克风处访问的隐私设置。
如果用户有允许隐私标志,我的应用程序可以正常工作,否则我的应用程序无法正常工作。那么如何通过c#查看这个隐私设置呢?
【问题讨论】:
似乎没有直接的方法来确定您的应用程序是否具有这些权限,因此您最好的选择是尝试访问麦克风并在错误发生时捕获错误。
try
{
// code to access microphone
}
catch (System.UnauthorizedAccessException e)
{
// notify user application can't work without microphone permission
}
【讨论】:
据我所知,这是解决问题的一种方法。不幸的是,它可能会捕获与麦克风隐私设置无关的其他错误。
/// <summary>
/// With Windows 10 update 1803 came an option to deny access to the microphones on an OS level.
/// The option covers all soundcards installed into the PC (Magnum/Callisto is a soundcard)
/// </summary>
public static class MicrophonePrivacyProbe
{
/// <summary>
/// Test if Microphone Privacy Settings are to restrictive for microphone access.
/// </summary>
/// <returns>True if microphone is accessible</returns>
public static bool Allowed()
{
bool access = false;
var devices = new CaptureDevicesCollection();
if ( devices?.Count <= 0 ) return false;
var captureDevice = new Capture(devices[0].DriverGuid);
CaptureBuffer applicationBuffer = null;
var inputFormat = new WaveFormat();
inputFormat.AverageBytesPerSecond = 8000;
inputFormat.BitsPerSample = 8;
inputFormat.BlockAlign = 1;
inputFormat.Channels = 1;
inputFormat.FormatTag = WaveFormatTag.Pcm;
inputFormat.SamplesPerSecond = 8000;
CaptureBufferDescription bufferdesc = new CaptureBufferDescription();
bufferdesc.BufferBytes = 200;
bufferdesc.Format = inputFormat;
try
{
applicationBuffer = new CaptureBuffer(bufferdesc, captureDevice);
access = true;
}
catch (SoundException e)
{
}
finally
{
applicationBuffer?.Dispose();
captureDevice?.Dispose();
}
return access;
}
}
【讨论】: