Ver Fonte

More clean-up

hellzerg há 7 anos atrás
pai
commit
a06682e57a
6 ficheiros alterados com 173 adições e 305 exclusões
  1. 7 0
      Optimizer/Enums.cs
  2. 53 126
      Optimizer/Integrator.cs
  3. 0 4
      Optimizer/Optimize.cs
  4. 0 1
      Optimizer/StartupItem.cs
  5. 112 171
      Optimizer/Utilities.cs
  6. 1 3
      README.md

+ 7 - 0
Optimizer/Enums.cs

@@ -20,6 +20,13 @@ namespace Optimizer
         Windows10
     }
 
+    internal enum RegistryHive
+    {
+        CurrentUser,
+        LocalMachine
+    }
+
+
     internal enum StartupItemLocation
     {
         Folder,

+ 53 - 126
Optimizer/Integrator.cs

@@ -50,15 +50,15 @@ namespace Optimizer
         internal static List<string> GetCustomCommands()
         {
             List<string> items = new List<string>();
-            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\");
 
-            foreach (string command in key.GetSubKeyNames())
+            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\"))
             {
-                items.Add(command);
+                foreach (string command in key.GetSubKeyNames())
+                {
+                    items.Add(command);
+                }
             }
 
-            key.Close();
-
             return items;
         }
 
@@ -69,27 +69,29 @@ namespace Optimizer
 
         private static void CreateDefaultCommand(string itemName)
         {
-            RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + itemName, true);
-            key.CreateSubKey("command", RegistryKeyPermissionCheck.Default);
-            key.Close();
+            using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + itemName, true))
+            {
+                key.CreateSubKey("command", RegistryKeyPermissionCheck.Default);
+            }        
         }
 
         internal static List<string> GetDesktopItems()
         {
             List<string> items = new List<string>();
-            RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", false);
 
-            foreach (string item in key.GetSubKeyNames())
+            using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", false))
             {
-                // filter the list, so the default items will not be visible
-                if (item.Contains("Gadgets")) continue;
-                if (item.Contains("Display")) continue;
-                if (item.Contains("Personalize")) continue;
+                foreach (string item in key.GetSubKeyNames())
+                {
+                    // filter the list, so the default items will not be visible
+                    if (item.Contains("Gadgets")) continue;
+                    if (item.Contains("Display")) continue;
+                    if (item.Contains("Personalize")) continue;
 
-                items.Add(item);
+                    items.Add(item);
+                }
             }
 
-            key.Close();
             return items;
         }
 
@@ -97,34 +99,30 @@ namespace Optimizer
         {
             try
             {
-                RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true);
-
-                try
+                using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
                 {
-                    key.DeleteSubKey(name + "\\command");
+                    try
+                    {
+                        key.DeleteSubKeyTree(name, false);
+                    }
+                    catch { }
                 }
-                catch { }
-                key.DeleteSubKey(name);
             }
             catch { }
         }
 
         internal static void RemoveAllItems(List<string> items)
         {
-            RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true);
-
-            foreach (string item in items)
+            using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
             {
-                try
+                foreach (string item in items)
                 {
                     try
                     {
-                        key.DeleteSubKey(item + "\\command");
+                        key.DeleteSubKeyTree(item, false);
                     }
                     catch { }
-                    key.DeleteSubKey(item);
                 }
-                catch { }
             }
         }
 
@@ -156,6 +154,7 @@ namespace Optimizer
                 if (url.HostNameType == UriHostNameType.Dns)
                 {
                     Image.FromStream(((HttpWebResponse)WebRequest.Create("http://" + url.Host + "/favicon.ico").GetResponse()).GetResponseStream()).Save(Required.FavIcons + name + ".ico", ImageFormat.Bmp);
+
                     favicon = Required.FavIcons + name + ".ico";
                 }
             }
@@ -166,119 +165,47 @@ namespace Optimizer
 
         internal static void AddItem(string name, string item, string icon, DesktopTypePosition position, bool shift, DesktopItemType type)
         {
-            switch (type)
+            using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
             {
-                case DesktopItemType.Program:
-                    RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true);
-                    key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
-                    key.Close();
-                    CreateDefaultCommand(name);
+                key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
+            }
 
-                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
-                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Icon", icon);
-                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Position", position.ToString());
+            CreateDefaultCommand(name);
 
-                    if (shift)
-                    {
-                        Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Extended", "");
-                    }
-                    else
-                    {
-                        RegistryKey key2 = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, true);
-                        key2.DeleteValue("Extended", false);
-                        key2.Close();
-                    }
+            if (shift)
+            {
+                Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Extended", "");
+            }
+            else
+            {
+                using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, true))
+                {
+                    key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
+                }
+            }
 
+            Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Icon", icon);
+            Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Position", position.ToString());
+
+            switch (type)
+            {
+                case DesktopItemType.Program:
+                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
                     break;
                 case DesktopItemType.Folder:
-                    RegistryKey key3 = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true);
-                    key3.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
-                    key3.Close();
-                    CreateDefaultCommand(name);
-
                     Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
-                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Icon", icon);
-                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Position", position.ToString());
-
-                    if (shift)
-                    {
-                        Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Extended", "");
-                    }
-                    else
-                    {
-                        RegistryKey key2 = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, true);
-                        key2.DeleteValue("Extended", false);
-                        key2.Close();
-                    }
-
                     break;
                 case DesktopItemType.Link:
-                    RegistryKey key4 = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true);
-                    key4.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
-                    key4.Close();
-                    CreateDefaultCommand(name);
-
                     Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
-                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Icon", icon);
-                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Position", position.ToString());
-
-                    if (shift)
-                    {
-                        Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Extended", "");
-                    }
-                    else
-                    {
-                        RegistryKey key2 = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, true);
-                        key2.DeleteValue("Extended", false);
-                        key2.Close();
-                    }
-
                     break;
                 case DesktopItemType.File:
-                    RegistryKey key5 = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true);
-                    key5.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
-                    key5.Close();
-                    CreateDefaultCommand(name);
-                    string def = @"""";
-                    string def2 = "explorer.exe";
-
-                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", def2 + " " + def + item + def);
-                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Icon", icon);
-                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Position", position.ToString());
-
-                    if (shift)
-                    {
-                        Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Extended", "");
-                    }
-                    else
-                    {
-                        RegistryKey key2 = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, true);
-                        key2.DeleteValue("Extended", false);
-                        key2.Close();
-                    }
+                    string tmp = @"""";
+                    string tmp2 = "explorer.exe";
 
+                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", tmp2 + " " + tmp + item + tmp);
                     break;
                 case DesktopItemType.Command:
-                    RegistryKey key6 = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true);
-                    key6.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
-                    key6.Close();
-                    CreateDefaultCommand(name);
-
                     Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
-                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Icon", icon);
-                    Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Position", position.ToString());
-
-                    if (shift)
-                    {
-                        Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Extended", "");
-                    }
-                    else
-                    {
-                        RegistryKey key2 = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, true);
-                        key2.DeleteValue("Extended", false);
-                        key2.Close();
-                    }
-
                     break;
             }
         }

+ 0 - 4
Optimizer/Optimize.cs

@@ -71,7 +71,6 @@ namespace Optimizer
         internal static void DisableMediaPlayerSharing()
         {
             Utilities.StopService("WMPNetworkSvc");
-
             Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WMPNetworkSvc", "Start", "4", RegistryValueKind.DWord);
         }
 
@@ -152,7 +151,6 @@ namespace Optimizer
         internal static void DisableErrorReporting()
         {
             Utilities.StopService("WerSvc");
-
             Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WerSvc", "Start", "4", RegistryValueKind.DWord);
         }
 
@@ -180,7 +178,6 @@ namespace Optimizer
         internal static void UninstallOneDrive()
         {
             Utilities.RunBatchFile(Required.RequiredFolder + "\\OneDrive_Uninstaller.cmd");
-
             Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\OneDrive", "DisableFileSyncNGSC", "1", RegistryValueKind.DWord);
         }
 
@@ -212,7 +209,6 @@ namespace Optimizer
         internal static void DisableWAPPush()
         {
             Utilities.StopService("dmwappushservice");
-
             Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\dmwappushservice", "Start", "4", RegistryValueKind.DWord);
         }
 

+ 0 - 1
Optimizer/StartupItem.cs

@@ -74,7 +74,6 @@ namespace Optimizer
                 Key.DeleteValue(Name, false);
             }
             catch { }
-            finally { Key.Close(); }
         }
 
         internal override void LocateFile()

+ 112 - 171
Optimizer/Utilities.cs

@@ -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)

+ 1 - 3
README.md

@@ -32,9 +32,7 @@ https://github.com/hellzerg/optimizer/releases
  
 Compatible with Windows 7, 8, 8.1, 10
 
-Also compatible with Windows Server 2008, 2012, 2016
-
-Does not work with Windows XP or Vista or Windows Server 2003
+Does not work with Windows XP or Vista
 
 ## Details: ##