win11 对于隐私操作管理和控制比较严格,导致应用去控制时比较麻烦。采用一种投机取巧的方案,修改注册表,让用户无法修改。然后通过代码的方式控制注册表实现摄像头的权限的控制
通过修改注册表项
/HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/PolicyManager/default/Privacy/LetAppsAccessCamera
状态说明:
1.当value为0的时候
相机权限控制为可更改状态,权限会根据用户选择而变更
2.当value为1的时候
相机权限不可修改,且固定为打开状态
3.当value为2的时候
相机权限不可修改,且固定为关闭状态
但是只是通过修改这个项后,我们发现系统中设置界面没有同步更新过来
在win11环境下 需要执行
C:/windows/system32/SystemSettingsAdminFlows.exe 这个应用 (需要管理员身份运行,如果是代码控制,需要x64)
参数
SetCamSystemGlobal webcam 0 表示关闭
SetCamSystemGlobal webcam 1 表示开启
这个值暂时不知道在哪获取
直接上代码了
public class CameraSecurity { private const string SystemSettingsAdminFlows = @"C:/Windows/System32/SystemSettingsAdminFlows.exe"; private const string LetAppsAccessCamera = @"SOFTWARE/Microsoft/PolicyManager/default/Privacy/LetAppsAccessCamera"; private const string Webcam = @"Software/Microsoft/Windows/CurrentVersion/CapabilityAccessManager/ConsentStore/webcam"; /// <summary> /// 恢复到系统默认的操作方案 /// 需要管理员权限 /// </summary> public void ResetToSystemDefault() { SetPolicy(0x00000000);//全局允许 } /// <summary> /// 全局允许访问 /// 需要管理员权限 /// </summary> public void AllowAccess() { SetPolicy(0x00000000);//全局允许 SetSecurityItem("Allow"); SetGlobalFlows(1);//全局通知 SetPolicy(0x00000001);//全局打开 } /// <summary> /// 全局禁止访问 /// 需要管理员权限 /// </summary> public void DenyAccess() { SetPolicy(0x00000000);//全局允许 SetSecurityItem("Deny"); SetGlobalFlows(0);//全局通知 SetPolicy(0x00000002);//全局禁用 } private RegistryKey LocalMachine => Environment.Is64BitOperatingSystem ? RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64) : Registry.LocalMachine; private RegistryKey CurrentUser => Environment.Is64BitOperatingSystem ? RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64) : Registry.CurrentUser; /// <summary> /// 设置全局策略 /// </summary> /// <param name="letAppsAccessCameraValue"></param> private void SetPolicy(int letAppsAccessCameraValue) { using (RegistryKey key = LocalMachine.OpenSubKey(LetAppsAccessCamera, true)) { if (key == null) { key.CreateSubKey(LetAppsAccessCamera, RegistryKeyPermissionCheck.ReadWriteSubTree); using (RegistryKey newKey = LocalMachine.OpenSubKey(LetAppsAccessCamera, true)) { newKey.SetValue("value", letAppsAccessCameraValue, RegistryValueKind.DWord); } return; } key.SetValue("value", letAppsAccessCameraValue, RegistryValueKind.DWord); } } /// <summary> /// 全局通知 /// </summary> /// <param name="globalParam"></param> private void SetGlobalFlows(int globalParam) { if (File.Exists(SystemSettingsAdminFlows)) { Process.Start(SystemSettingsAdminFlows, $"SetCamSystemGlobal webcam {globalParam}"); } } /// <summary> /// 修改每一项 /// </summary> /// <param name="webCamParam"></param> private void SetSecurityItem(string webCamParam) { using (RegistryKey key = CurrentUser.OpenSubKey(Webcam, true)) { if (key == null) return; key.SetValue("Value", webCamParam); foreach (var appKeyName in key.GetSubKeyNames()) { if (string.IsNullOrWhiteSpace(appKeyName)) continue; using (RegistryKey appKey = key.OpenSubKey(appKeyName, true)) { if (appKey != null) appKey.SetValue("Value", webCamParam); } } } } }
原创文章,作者:sunnyman218,如若转载,请注明出处:https://blog.ytso.com/245721.html