namespace WinControl
{
using System;
using System.Management;
using Helpers;
using Microsoft.Win32;
public static class RegControl
{
/// <summary>
/// Метод для проверки битности реестра
/// </summary>
private static RegistryView Regview => Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
public static bool ToogleCmd(bool write)
{
const string REG = @"SOFTWARE\Policies\Microsoft\Windows";
try
{
using RegistryKey key = Registry.CurrentUser.OpenSubKey(REG, true); // Открываем раздел реестра для записи
using RegistryKey sxs = key?.CreateSubKey("System"); // Тут создаём папку System
sxs?.SetValue("DisableCMD", write ? 1 : 0); // True - Блочит\False - Разблочит
return true;
}
catch { return false; } // Тут можно по желанию добавить проверку на доступность
}
public static bool ToogleUser(bool write, string value)
{
const string REG = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies";
try
{
using RegistryKey key = Registry.CurrentUser.OpenSubKey(REG, true);
using RegistryKey sxs = key?.CreateSubKey("System");
if (value.Equals("Uac", StringComparison.CurrentCultureIgnoreCase))
{
sxs?.SetValue("EnableLUA", 0, RegistryValueKind.DWord);
sxs?.SetValue("PromptOnSecureDesktop", 0, RegistryValueKind.DWord);
}
if (value.Equals("TaskMgr", StringComparison.CurrentCultureIgnoreCase))
{
sxs?.SetValue("DisableTaskMgr", write ? 1 : 0, RegistryValueKind.DWord);
}
if (value.Equals("DisableRegistryTools", StringComparison.CurrentCultureIgnoreCase))
{
sxs?.SetValue("DisableRegistryTools", write ? 1 : 0, RegistryValueKind.DWord);
}
return true;
}
catch { return false; }
}
public static bool ToogleTaskBar(bool write)
{
const int SW_HIDE = 0, SW_SHOW = 1;
try
{
IntPtr TaskBarHWnd = NativeMethods.FindWindow("Shell_TrayWnd", "");
if (TaskBarHWnd != null)
{
NativeMethods.ShowWindow(TaskBarHWnd, write ? SW_HIDE : SW_SHOW);
const string REG = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer";
using RegistryKey registry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Regview);
using RegistryKey key = registry.OpenSubKey(REG, true);
key?.SetValue("TaskbarLockAll", write ? 1 : 0, RegistryValueKind.DWord);
WinXplorer.Refresh();
return true;
}
return false;
}
catch { return false; }
}
public static bool ToogleUac(bool write)
{
try
{
const string REG = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System";
using RegistryKey registry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Regview);
using RegistryKey key = registry.OpenSubKey(REG, true);
key?.SetValue("EnableLUA", write ? 1 : 0, RegistryValueKind.DWord);
key?.SetValue("PromptOnSecureDesktop", write ? 1 : 0, RegistryValueKind.DWord);
key?.SetValue("ConsentPromptBehaviorAdmin", write ? 5 : 0 , RegistryValueKind.DWord);
WinXplorer.Refresh();
return true;
}
catch { return false; }
}
public static bool ToogleSmart(bool write)
{
try
{
const string REG = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer";
using RegistryKey registry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Regview);
using RegistryKey key = registry.OpenSubKey(REG, true);
key?.SetValue("SmartScreenEnabled", write ? "on" : "off");
WinXplorer.Refresh();
return true;
}
catch { return false; }
}
}
}