[英]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.