[英]How can I check Windows Lock settings?
我有一个Windows窗体,该窗体需要能够检查Windows屏幕保护程序设置(是否处于活动状态,是否在15分钟之内,是否处于“恢复状态,显示登录状态”处于活动状态),如果所有这些都正确,则用户会得到一个不错的选择如果不通过,则大PASS用户会在文本框中看到大故障。 我在网上看过,却发现没有任何东西可以做到这一点。 我当时以为我会通过Windows注册表检查设置,因为无论如何我都必须这样做。
(我曾经使用BAT来完成此任务,我将在下文中对此进行发布,但我需要与vista兼容的内容并以用户友好的方式显示结果)
echo The following check is for 15 minute inactive lock
echo if true the setting will be set to 1 of false 0
echo
echo is the screen saver active
reg query "HKCU\Control Panel\Desktop" /v ScreenSaveActive
echo is the screen saver locking the device
reg query "HKCU\Control Panel\Desktop" /v ScreenSaverIsSecure
echo How long until the screen saver activates (900 or below for compliance)
reg query "HKCU\Control Panel\Desktop" /v ScreenSaveTimeOut
reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\GroupPolicyObjects"
添加到您的代码页: using Microsoft.Win32;
然后使用以下代码从注册表中提取值:
using (var key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop"))
{
if (key != null)
{
Boolean isActive = false;
Boolean isSecure = false;
Int32 timeoutSeconds = 0;
var o = key.GetValue("ScreenSaveActive", "");
if (!String.IsNullOrWhiteSpace(o as String))
{
Int32 i;
if (Int32.TryParse((String)o, out i))
isActive = (i == 1);
}
o = key.GetValue("ScreenSaverIsSecure", "");
if (!String.IsNullOrWhiteSpace(o as String))
{
Int32 i;
if (Int32.TryParse((String)o, out i))
isSecure = (i == 1);
}
o = key.GetValue("ScreenSaveTimeOut", "");
if (!String.IsNullOrWhiteSpace(o as String))
{
Int32 i;
if (Int32.TryParse((String)o, out i))
timeoutSeconds = i;
}
}
}
正如Ron Beyer在评论中指出的那样,仅ScreenSaveActive
标志可能并不可靠。 可能是您必须一起考虑这些标志才能获得完整的图片。
请注意,所有的字符串解析都是由于值存储为REG_SZ
而不是数字, 这一点对此链接进行了介绍。
听起来您正在尝试读取注册表项并根据该值设置布尔值。 如果是这样,这应该会有所帮助:
RegistryKey regDesktop = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop");
bool isScreenSaverActive = regDesktop != null &&
regDesktop.GetValue("ScreenSaveActive").ToString() == "1";
如果您在问题中描述的设置是唯一要检查的设置,则此方法将在存在且与您的期望值匹配(ScreenSaveActive == 1, ScreenSaverIsSecure == 1, and ScreenSaveTimeOut <= 900)
时返回true:
public static bool LockSettingsAreCompliant()
{
bool lockSettingsAreCompliant = false;
try
{
using (var regKey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop"))
{
lockSettingsAreCompliant =
regKey.GetValue("ScreenSaveActive").ToString() == "1" &&
regKey.GetValue("ScreenSaverIsSecure").ToString() == "1" &&
int.Parse(regKey.GetValue("ScreenSaveTimeOut").ToString()) <= 900;
}
}
catch
{
// Swallow exceptions and let the method return false
}
return lockSettingsAreCompliant;
}
然后,您可以像下面这样使用此方法来设置文本框文本:
screenSaver.Text = LockSettingsAreCompliant() ? "PASS" : "FAIL";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.