简体   繁体   English

如何检查Windows Lock设置?

[英]How can I check Windows Lock settings?

I have a windows form that needs to be able to check the windows screen saver settings (Is it active, is it under 15 minutes, is "on resume, display logon" active) Essentially if all of those are true the user gets a nice big PASS if not the user gets a big FAIL in a textbox. 我有一个Windows窗体,该窗体需要能够检查Windows屏幕保护程序设置(是否处于活动状态,是否在15分钟之内,是否处于“恢复状态,显示登录状态”处于活动状态),如果所有这些都正确,则用户会得到一个不错的选择如果不通过,则大PASS用户会在文本框中看到大故障。 I have looked online and have found nothing that accomplishes this. 我在网上看过,却发现没有任何东西可以做到这一点。 I was thinking I would check the settings through windows registry since I will have to do this method of other system settings anyway. 我当时以为我会通过Windows注册表检查设置,因为无论如何我都必须这样做。

(I used to use a BAT to accomplish this which I will post below for context, but I need somethings compatible with vista and up that displays the results in a user-friendly manner) (我曾经使用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"

Add to your code page: using Microsoft.Win32; 添加到您的代码页: using Microsoft.Win32;

Then use this code to pull values from the registry: 然后使用以下代码从注册表中提取值:

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

As Ron Beyer pointed out in comments, the ScreenSaveActive flag alone might not be reliable. 正如Ron Beyer在评论中指出的那样,仅ScreenSaveActive标志可能并不可靠。 It might be that you have to consider the flags together to get the complete picture. 可能是您必须一起考虑这些标志才能获得完整的图片。

Note that all of the string parsing is due to the fact that the values are stored as REG_SZ , and not numeric, as this link describes for one of them. 请注意,所有的字符串解析都是由于值存储为REG_SZ而不是数字, 这一点对此链接进行了介绍。

It sounds like you're trying to read a registry key and set a Boolean based on the value. 听起来您正在尝试读取注册表项并根据该值设置布尔值。 If that's the case, this should help: 如果是这样,这应该会有所帮助:

RegistryKey regDesktop = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop");

bool isScreenSaverActive = regDesktop != null && 
    regDesktop.GetValue("ScreenSaveActive").ToString() == "1";

If the settings you described in your question are the only ones you want to check, here's a method that will return true if they exist and match your expected values (ScreenSaveActive == 1, ScreenSaverIsSecure == 1, and ScreenSaveTimeOut <= 900) : 如果您在问题中描述的设置是唯一要检查的设置,则此方法将在存在且与您的期望值匹配(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;
}

And then you can use this method like the following to set the textbox text: 然后,您可以像下面这样使用此方法来设置文本框文本:

screenSaver.Text = LockSettingsAreCompliant() ? "PASS" : "FAIL";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM