【问题标题】:Check microphone privacy setting on WPF检查 WPF 上的麦克风隐私设置
【发布时间】:2018-08-10 14:09:47
【问题描述】:

我有一个用 c# 编写的 WPF 应用程序,它使用带有 naudio 库的麦克风设备,在 Windows 10 更新版本 1803 上添加了在麦克风处访问的隐私设置。

如果用户有允许隐私标志,我的应用程序可以正常工作,否则我的应用程序无法正常工作。那么如何通过c#查看这个隐私设置呢?

【问题讨论】:

    标签: c# wpf windows winapi


    【解决方案1】:

    似乎没有直接的方法来确定您的应用程序是否具有这些权限,因此您最好的选择是尝试访问麦克风并在错误发生时捕获错误。

    try
    {
    // code to access microphone
    }
    catch (System.UnauthorizedAccessException e)
    {
    // notify user application can't work without microphone permission                
    }
    

    【讨论】:

    • 如果你有注册表访问权限,你可以这样做,可能是因为它的配置数据存储在那里,但我不知道 OP 在他的情况下是否这样做。我猜有 .NET API 用于工作注册表并且总是 P/Invoke 到 Win32 API。不过一般来说,此类事情的大多数配置数据都存储在注册表中。
    【解决方案2】:

    据我所知,这是解决问题的一种方法。不幸的是,它可能会捕获与麦克风隐私设置无关的其他错误。

    /// <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;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多