|
@@ -93,9 +93,7 @@ namespace Optimizer
|
|
|
|
|
|
internal static bool IsAdmin()
|
|
|
{
|
|
|
- var identity = WindowsIdentity.GetCurrent();
|
|
|
- var principal = new WindowsPrincipal(identity);
|
|
|
- return principal.IsInRole(WindowsBuiltInRole.Administrator);
|
|
|
+ return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
|
|
|
}
|
|
|
|
|
|
internal static bool IsCompatible()
|
|
@@ -167,7 +165,7 @@ namespace Optimizer
|
|
|
|
|
|
p.WaitForExit();
|
|
|
}
|
|
|
- catch (Exception)
|
|
|
+ catch
|
|
|
{
|
|
|
p.Dispose();
|
|
|
}
|
|
@@ -218,176 +216,98 @@ namespace Optimizer
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- internal static List<StartupItem> GetStartupItems()
|
|
|
+ private static void GetRegistryStartupItemsHelper(ref List<StartupItem> list, StartupItemLocation location, StartupItemType type)
|
|
|
{
|
|
|
- List<StartupItem> collection = new List<StartupItem>();
|
|
|
- RegistryKey registryKey = null;
|
|
|
+ string keyPath = string.Empty;
|
|
|
+ RegistryKey hive = null;
|
|
|
|
|
|
- // Get Local Machine Run startup items
|
|
|
- try
|
|
|
+ if (location == StartupItemLocation.HKLM)
|
|
|
{
|
|
|
- registryKey = Registry.LocalMachine.OpenSubKey(LocalMachineRun, true);
|
|
|
- string[] valueNames = registryKey.GetValueNames();
|
|
|
+ hive = Registry.LocalMachine;
|
|
|
|
|
|
- foreach (string s in valueNames)
|
|
|
+ if (type == StartupItemType.Run)
|
|
|
{
|
|
|
- RegistryStartupItem item = new RegistryStartupItem();
|
|
|
- item.Name = s;
|
|
|
- item.FileLocation = registryKey.GetValue(s).ToString();
|
|
|
- item.Key = registryKey;
|
|
|
- item.RegistryLocation = StartupItemLocation.HKLM;
|
|
|
- item.StartupType = StartupItemType.Run;
|
|
|
-
|
|
|
- collection.Add(item);
|
|
|
+ keyPath = LocalMachineRun;
|
|
|
}
|
|
|
- }
|
|
|
- catch { }
|
|
|
-
|
|
|
- // Get Local Machine Run Once startup items
|
|
|
- try
|
|
|
- {
|
|
|
- registryKey = Registry.LocalMachine.OpenSubKey(LocalMachineRunOnce, true);
|
|
|
- string[] valueNames = registryKey.GetValueNames();
|
|
|
-
|
|
|
- foreach (string s in valueNames)
|
|
|
+ else if (type == StartupItemType.RunOnce)
|
|
|
{
|
|
|
- RegistryStartupItem item = new RegistryStartupItem();
|
|
|
- item.Name = s;
|
|
|
- item.FileLocation = registryKey.GetValue(s).ToString();
|
|
|
- item.Key = registryKey;
|
|
|
- item.RegistryLocation = StartupItemLocation.HKLM;
|
|
|
- item.StartupType = StartupItemType.RunOnce;
|
|
|
-
|
|
|
- collection.Add(item);
|
|
|
+ keyPath = LocalMachineRunOnce;
|
|
|
}
|
|
|
}
|
|
|
- catch { }
|
|
|
-
|
|
|
- // Get Local Machine Run WoW startup items
|
|
|
- try
|
|
|
+ else if (location == StartupItemLocation.HKLMWoW)
|
|
|
{
|
|
|
- registryKey = Registry.LocalMachine.OpenSubKey(LocalMachineRunWoW, true);
|
|
|
- string[] valueNames2 = registryKey.GetValueNames();
|
|
|
+ hive = Registry.LocalMachine;
|
|
|
|
|
|
- foreach (string s in valueNames2)
|
|
|
+ if (type == StartupItemType.Run)
|
|
|
{
|
|
|
- RegistryStartupItem item = new RegistryStartupItem();
|
|
|
- item.Name = s;
|
|
|
- item.FileLocation = registryKey.GetValue(s).ToString();
|
|
|
- item.Key = registryKey;
|
|
|
- item.RegistryLocation = StartupItemLocation.HKLMWoW;
|
|
|
- item.StartupType = StartupItemType.Run;
|
|
|
-
|
|
|
- collection.Add(item);
|
|
|
+ keyPath = LocalMachineRunWoW;
|
|
|
}
|
|
|
- }
|
|
|
- catch { }
|
|
|
-
|
|
|
- // Get Local Machine Run Once WoW startup items
|
|
|
- try
|
|
|
- {
|
|
|
- registryKey = Registry.LocalMachine.OpenSubKey(LocalMachineRunOnceWow, true);
|
|
|
- string[] valueNames2 = registryKey.GetValueNames();
|
|
|
-
|
|
|
- foreach (string s in valueNames2)
|
|
|
+ else if (type == StartupItemType.RunOnce)
|
|
|
{
|
|
|
- RegistryStartupItem item = new RegistryStartupItem();
|
|
|
- item.Name = s;
|
|
|
- item.FileLocation = registryKey.GetValue(s).ToString();
|
|
|
- item.Key = registryKey;
|
|
|
- item.RegistryLocation = StartupItemLocation.HKLMWoW;
|
|
|
- item.StartupType = StartupItemType.RunOnce;
|
|
|
-
|
|
|
- collection.Add(item);
|
|
|
+ keyPath = LocalMachineRunOnceWow;
|
|
|
}
|
|
|
}
|
|
|
- catch { }
|
|
|
-
|
|
|
- // Get Current User Run startup items
|
|
|
- try
|
|
|
+ else if (location == StartupItemLocation.HKCU)
|
|
|
{
|
|
|
- registryKey = Registry.CurrentUser.OpenSubKey(CurrentUserRun, true);
|
|
|
- string[] valueNames3 = registryKey.GetValueNames();
|
|
|
+ hive = Registry.CurrentUser;
|
|
|
|
|
|
- foreach (string s in valueNames3)
|
|
|
+ if (type == StartupItemType.Run)
|
|
|
+ {
|
|
|
+ keyPath = CurrentUserRun;
|
|
|
+ }
|
|
|
+ else if (type == StartupItemType.RunOnce)
|
|
|
{
|
|
|
- RegistryStartupItem item = new RegistryStartupItem();
|
|
|
- item.Name = s;
|
|
|
- item.FileLocation = registryKey.GetValue(s).ToString();
|
|
|
- item.Key = registryKey;
|
|
|
- item.RegistryLocation = StartupItemLocation.HKCU;
|
|
|
- item.StartupType = StartupItemType.Run;
|
|
|
-
|
|
|
- collection.Add(item);
|
|
|
+ keyPath = CurrentUserRunOnce;
|
|
|
}
|
|
|
}
|
|
|
- catch { }
|
|
|
|
|
|
- // Get Current User Run Once startup items
|
|
|
- try
|
|
|
+ if (hive != null)
|
|
|
{
|
|
|
- registryKey = Registry.CurrentUser.OpenSubKey(CurrentUserRunOnce, true);
|
|
|
- string[] valueNames3 = registryKey.GetValueNames();
|
|
|
-
|
|
|
- foreach (string s in valueNames3)
|
|
|
+ RegistryKey key = hive.OpenSubKey(keyPath, true);
|
|
|
+
|
|
|
+ if (key != null)
|
|
|
{
|
|
|
- RegistryStartupItem item = new RegistryStartupItem();
|
|
|
- item.Name = s;
|
|
|
- item.FileLocation = registryKey.GetValue(s).ToString();
|
|
|
- item.Key = registryKey;
|
|
|
- item.RegistryLocation = StartupItemLocation.HKCU;
|
|
|
- item.StartupType = StartupItemType.RunOnce;
|
|
|
-
|
|
|
- collection.Add(item);
|
|
|
+ string[] valueNames = key.GetValueNames();
|
|
|
+
|
|
|
+ foreach (string x in valueNames)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ RegistryStartupItem item = new RegistryStartupItem();
|
|
|
+ item.Name = x;
|
|
|
+ item.FileLocation = key.GetValue(x).ToString();
|
|
|
+ item.Key = key;
|
|
|
+ item.RegistryLocation = location;
|
|
|
+ item.StartupType = type;
|
|
|
+
|
|
|
+ list.Add(item);
|
|
|
+ }
|
|
|
+ catch { }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- catch { }
|
|
|
-
|
|
|
- registryKey.Dispose();
|
|
|
+ }
|
|
|
|
|
|
- // Get Current User Startup folder startup items
|
|
|
- try
|
|
|
+ private static void GetFolderStartupItemsHelper(ref List<StartupItem> list, string[] files, string[] shortcuts)
|
|
|
+ {
|
|
|
+ foreach (string file in files)
|
|
|
{
|
|
|
- // get shortcuts to files
|
|
|
- string[] shortcuts = Directory.GetFiles(CurrentUserStartupFolder, "*.lnk", SearchOption.AllDirectories);
|
|
|
-
|
|
|
- // get executables
|
|
|
- string[] files = Directory.GetFiles(CurrentUserStartupFolder, "*.exe", SearchOption.AllDirectories);
|
|
|
-
|
|
|
- foreach (string shortcut in shortcuts)
|
|
|
+ try
|
|
|
{
|
|
|
FolderStartupItem item = new FolderStartupItem();
|
|
|
- item.Name = Path.GetFileNameWithoutExtension(shortcut);
|
|
|
- item.FileLocation = GetShortcutTargetFile(shortcut);
|
|
|
- item.Shortcut = shortcut;
|
|
|
+ item.Name = Path.GetFileNameWithoutExtension(file);
|
|
|
+ item.FileLocation = file;
|
|
|
+ item.Shortcut = file;
|
|
|
item.RegistryLocation = StartupItemLocation.Folder;
|
|
|
|
|
|
- collection.Add(item);
|
|
|
- }
|
|
|
-
|
|
|
- foreach (string file in files)
|
|
|
- {
|
|
|
- FolderStartupItem item2 = new FolderStartupItem();
|
|
|
- item2.Name = Path.GetFileNameWithoutExtension(file);
|
|
|
- item2.FileLocation = file;
|
|
|
- item2.Shortcut = file;
|
|
|
- item2.RegistryLocation = StartupItemLocation.Folder;
|
|
|
-
|
|
|
- collection.Add(item2);
|
|
|
+ list.Add(item);
|
|
|
}
|
|
|
+ catch { }
|
|
|
}
|
|
|
- catch { }
|
|
|
|
|
|
- // Get Local Machine Startup folder startup items
|
|
|
- try
|
|
|
+ foreach (string shortcut in shortcuts)
|
|
|
{
|
|
|
- // get shortcuts to files
|
|
|
- string[] shortcuts = Directory.GetFiles(LocalMachineStartupFolder, "*.lnk", SearchOption.AllDirectories);
|
|
|
-
|
|
|
- // get executables
|
|
|
- string[] files = Directory.GetFiles(LocalMachineStartupFolder, "*.exe", SearchOption.AllDirectories);
|
|
|
-
|
|
|
- foreach (string shortcut in shortcuts)
|
|
|
+ try
|
|
|
{
|
|
|
FolderStartupItem item = new FolderStartupItem();
|
|
|
item.Name = Path.GetFileNameWithoutExtension(shortcut);
|
|
@@ -395,23 +315,37 @@ namespace Optimizer
|
|
|
item.Shortcut = shortcut;
|
|
|
item.RegistryLocation = StartupItemLocation.Folder;
|
|
|
|
|
|
- collection.Add(item);
|
|
|
+ list.Add(item);
|
|
|
}
|
|
|
+ catch { }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- foreach (string file in files)
|
|
|
- {
|
|
|
- FolderStartupItem item2 = new FolderStartupItem();
|
|
|
- item2.Name = Path.GetFileNameWithoutExtension(file);
|
|
|
- item2.FileLocation = file;
|
|
|
- item2.Shortcut = file;
|
|
|
- item2.RegistryLocation = StartupItemLocation.Folder;
|
|
|
+ internal static List<StartupItem> GetStartupItems()
|
|
|
+ {
|
|
|
+ List<StartupItem> startupItems = new List<StartupItem>();
|
|
|
|
|
|
- collection.Add(item2);
|
|
|
- }
|
|
|
+ GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLM, StartupItemType.Run);
|
|
|
+ GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLM, StartupItemType.RunOnce);
|
|
|
+
|
|
|
+ GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKCU, StartupItemType.Run);
|
|
|
+ GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKCU, StartupItemType.RunOnce);
|
|
|
+
|
|
|
+ if (Environment.Is64BitOperatingSystem)
|
|
|
+ {
|
|
|
+ GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLMWoW, StartupItemType.Run);
|
|
|
+ GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLMWoW, StartupItemType.RunOnce);
|
|
|
}
|
|
|
- catch { }
|
|
|
|
|
|
- return collection;
|
|
|
+ string[] currentUserFiles = Directory.GetFiles(CurrentUserStartupFolder, "*.exe", SearchOption.AllDirectories);
|
|
|
+ string[] currentUserShortcuts = Directory.GetFiles(CurrentUserStartupFolder, "*.lnk", SearchOption.AllDirectories);
|
|
|
+ GetFolderStartupItemsHelper(ref startupItems, currentUserFiles, currentUserShortcuts);
|
|
|
+
|
|
|
+ string[] localMachineFiles = Directory.GetFiles(LocalMachineStartupFolder, "*.exe", SearchOption.AllDirectories);
|
|
|
+ string[] localMachineShortcuts = Directory.GetFiles(LocalMachineStartupFolder, "*.lnk", SearchOption.AllDirectories);
|
|
|
+ GetFolderStartupItemsHelper(ref startupItems, localMachineFiles, localMachineShortcuts);
|
|
|
+
|
|
|
+ return startupItems;
|
|
|
}
|
|
|
|
|
|
internal static void EnableFirewall()
|
|
@@ -421,51 +355,58 @@ namespace Optimizer
|
|
|
|
|
|
internal static void EnableCommandPrompt()
|
|
|
{
|
|
|
- RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Policies\\Microsoft\\Windows\\System");
|
|
|
- key.SetValue("DisableCMD", 0, RegistryValueKind.DWord);
|
|
|
- key.Close();
|
|
|
+ using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Policies\\Microsoft\\Windows\\System"))
|
|
|
+ {
|
|
|
+ key.SetValue("DisableCMD", 0, RegistryValueKind.DWord);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal static void EnableControlPanel()
|
|
|
{
|
|
|
- RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
|
|
|
- key.SetValue("NoControlPanel", 0, RegistryValueKind.DWord);
|
|
|
- key.Close();
|
|
|
+ using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
|
|
|
+ {
|
|
|
+ key.SetValue("NoControlPanel", 0, RegistryValueKind.DWord);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal static void EnableFolderOptions()
|
|
|
{
|
|
|
- RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
|
|
|
- key.SetValue("NoFolderOptions", 0, RegistryValueKind.DWord);
|
|
|
- key.Close();
|
|
|
+ using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
|
|
|
+ {
|
|
|
+ key.SetValue("NoFolderOptions", 0, RegistryValueKind.DWord);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal static void EnableRunDialog()
|
|
|
{
|
|
|
- RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
|
|
|
- key.SetValue("NoRun", 0, RegistryValueKind.DWord);
|
|
|
- key.Close();
|
|
|
+ using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
|
|
|
+ {
|
|
|
+ key.SetValue("NoRun", 0, RegistryValueKind.DWord);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal static void EnableContextMenu()
|
|
|
{
|
|
|
- RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
|
|
|
- key.SetValue("NoViewContextMenu", 0, RegistryValueKind.DWord);
|
|
|
- key.Close();
|
|
|
+ using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
|
|
|
+ {
|
|
|
+ key.SetValue("NoViewContextMenu", 0, RegistryValueKind.DWord);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal static void EnableTaskManager()
|
|
|
{
|
|
|
- RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
|
|
|
- key.SetValue("DisableTaskMgr", 0, RegistryValueKind.DWord);
|
|
|
- key.Close();
|
|
|
+ using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
|
|
|
+ {
|
|
|
+ key.SetValue("DisableTaskMgr", 0, RegistryValueKind.DWord);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal static void EnableRegistryEditor()
|
|
|
{
|
|
|
- RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
|
|
|
- key.SetValue("DisableRegistryTools", 0, RegistryValueKind.DWord);
|
|
|
- key.Close();
|
|
|
+ using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
|
|
|
+ {
|
|
|
+ key.SetValue("DisableRegistryTools", 0, RegistryValueKind.DWord);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal static void RunCommand(string command)
|