private void MultiSelectFirewallFiles(string[] array)
        {
            this.Invoke(new Action(() =>
            {
                progressBar1.Enabled = true;

                progressBar1.Style = ProgressBarStyle.Marquee;

                PauseButton.Enabled = false;
                StopButton.Enabled  = false;

                label3.Text = "Task:";

                label2.Text = "Firewall";

                label1.Text = "Add files to Windows Defender Firewall inbound and outbound rules.";
            }));

            foreach (string item in array)
            {
                try
                {
                    MainFolderName = Path.GetFileName(item);

                    string path      = Path.GetDirectoryName(item);
                    string title     = Path.GetFileName(path) + " - " + Path.GetFileName(item);
                    string arguments = "advfirewall firewall add rule name=" + "\"" + MainFolderName + " - " + title + "\"" + " dir=out program=" + "\"" + item + "\"" + " action=block";

                    StartProcess.StartInfo("netsh.exe", arguments, true, true, true);

                    path      = Path.GetDirectoryName(item);
                    title     = Path.GetFileName(path) + " - " + Path.GetFileName(item);
                    arguments = "advfirewall firewall add rule name=" + "\"" + MainFolderName + " - " + title + "\"" + " dir=in program=" + "\"" + item + "\"" + " action=block";

                    StartProcess.StartInfo("netsh.exe", arguments, true, true, true);
                }
                catch (Exception ex)
                {
                    MessageForm(ex.Message + Environment.NewLine + ex.StackTrace, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    NoErrors = false;
                }
            }
            this.Invoke(new Action(() =>
            {
                Hide();
                if (NoErrors)
                {
                    DialogResult results = MessageForm(Resources.DialogMessageBlockFiles, Resources.DialogTitleSuccess, MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
                    if (results == DialogResult.Yes)
                    {
                        StartProcess.StartInfo("wf.msc");
                    }
                }
            }));

            Environment.Exit(0);
        }
        public Settings()
        {
            InitializeComponent();
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            FileVersionInfo            fvi      = FileVersionInfo.GetVersionInfo(assembly.Location);
            string version = fvi.FileVersion;

            label1.Text = Resources.Version + version;

            RegistryKey subKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MenuTools");

            if (subKey == null)
            {
                StartProcess.StartInfo(GetAssembly.AssemblyInformation("directory") + @"\MenuTools.exe", "-install", false, true);
                Environment.Exit(0);
            }
            GetSettings();
        }
        private static void InstallerUnelevated()
        {
            try
            {
                RegistryKey subKey = null;
                if (ArchitectureCheck.ProcessorIs64Bit())
                {
                    subKey = Registry.LocalMachine.OpenSubKey(Resources.RegUninstallKey64String);
                }
                else
                {
                    subKey = Registry.LocalMachine.OpenSubKey(Resources.RegUninstallKey32String);
                }
                if (subKey == null)
                {
                    DialogResult results = MessageForm(Resources.InstallQuestion + Resources.MenuTools + Resources.InstallNotice, Resources.MenuTools, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                    if (results == DialogResult.Yes)
                    {
                        StartProcess.StartInfo(GetAssembly.AssemblyInformation("filelocation"), "-install", false, true);
                        Environment.Exit(0);
                    }
                }
                else
                {
                    DialogResult results = MessageForm(Resources.UninstallQuestion + Resources.MenuTools + Resources.UninstallNotice, Resources.MenuTools, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (results == DialogResult.Yes)
                    {
                        StartProcess.StartInfo(GetAssembly.AssemblyInformation("filelocation"), "-uninstall", false, true);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageForm(ex.Message + Environment.NewLine + ex.StackTrace, Resources.MenuTools, MessageBoxButtons.OK, MessageBoxIcon.Error);

                Environment.Exit(0);
            }
        }
        private void Uninstaller()
        {
            try
            {
                RegistryKey RegUninstallKey64 = Registry.LocalMachine.OpenSubKey(Resources.Uninstall64Bit, true);
                RegistryKey RegUninstallKey32 = Registry.LocalMachine.OpenSubKey(Resources.Uninstall32Bit, true);
                RegistryKey RegistrySoftware  = Registry.CurrentUser.OpenSubKey("SOFTWARE", true);
                RegistryKey UninstallInfo     = RegUninstallKey64;
                if (ArchitectureCheck.ProcessorIs64Bit())
                {
                    UninstallInfo = RegUninstallKey64;
                }
                if (!ArchitectureCheck.ProcessorIs64Bit())
                {
                    UninstallInfo = RegUninstallKey32;
                }
                StartProcess.StartInfo(@"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe", "-unregister " + "\"" + GetAssembly.AssemblyInformation("directory") + "\\MenuTools.dll" + "\"", true, true, true);

                UninstallInfo.DeleteSubKeyTree("MenuTools", false);
                RegistrySoftware.DeleteSubKey("MenuTools\\Settings", false);
                RegistrySoftware.DeleteSubKey("MenuTools", false);

                // Restart Explorer
                DialogResult = MessageForm(Resources.UninstallComplete, Resources.MenuTools, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (DialogResult == DialogResult.Yes)
                {
                    foreach (var proc in Process.GetProcessesByName("explorer"))
                    {
                        proc.Kill();

                        proc.WaitForExit();
                    }
                }

                // Delete shortcut
                StringBuilder allUserProfile = new StringBuilder(260);
                NativeMethods.SHGetSpecialFolderPath(IntPtr.Zero, allUserProfile, NativeMethods.CSIDL_COMMON_STARTMENU, false);
                string programs_path  = Path.Combine(allUserProfile.ToString(), "Programs");
                string shortcutFolder = Path.Combine(programs_path, @"MenuTools");
                foreach (string file in Directory.GetFiles(shortcutFolder))
                {
                    File.Delete(file);
                }
                Directory.Delete(shortcutFolder, true);

                try
                {
                    File.Copy(GetAssembly.AssemblyInformation("directory") + @"\Deleter.exe", Path.GetTempPath() + "Deleter.exe", true);

                    using (Process p = new Process())
                    {
                        p.StartInfo.Arguments = "\"" + GetAssembly.AssemblyInformation("directory") + "\"";
                        p.StartInfo.FileName  = Path.GetTempPath() + @"\Deleter.exe";
                        p.Start();
                    }
                }
                catch (Exception ex)
                {
                    MessageForm(ex.Message + "\n\nNot all files could be removed. You will need to remove those files manually.", "MenuTools", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                MessageForm(ex.Message + Environment.NewLine + ex.StackTrace, Resources.MenuTools, MessageBoxButtons.OK, MessageBoxIcon.Error);

                Environment.Exit(0);
            }
        }
        private void Installer(string location)
        {
            try
            {
                RegistryKey MenuToolsSettings = Registry.CurrentUser.CreateSubKey("SOFTWARE\\MenuTools\\Settings");
                RegistryKey InstallInfo       = null;
                if (ArchitectureCheck.ProcessorIs64Bit())
                {
                    RegistryKey RegUninstallKey64 = Registry.LocalMachine.CreateSubKey(Resources.RegUninstallKey64String);
                    InstallInfo = RegUninstallKey64;
                }
                if (!ArchitectureCheck.ProcessorIs64Bit())
                {
                    RegistryKey RegUninstallKey32 = Registry.LocalMachine.CreateSubKey(Resources.RegUninstallKey32String);
                    InstallInfo = RegUninstallKey32;
                }
                StartProcess.StartInfo(@"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe", "\"" + location + "\\MenuTools.dll" + "\"" + " -codebase", true, true, true);

                // Adds Information to Uninstall - Change to 32 bit for compiling x86
                InstallInfo.SetValue("InstallLocation", "\"" + location + "\"", RegistryValueKind.String);
                InstallInfo.SetValue("InstallFileLocation", "\"" + location + @"\MenuTools.exe" + "\"", RegistryValueKind.String);
                InstallInfo.SetValue("UninstallString", "\"" + location + @"\MenuTools.exe" + "\"" + " -uninstall", RegistryValueKind.String);
                InstallInfo.SetValue("DisplayIcon", location + @"\MenuTools.exe", RegistryValueKind.String);
                InstallInfo.SetValue("Publisher", "xCONFLiCTiONx", RegistryValueKind.String);
                InstallInfo.SetValue("HelpLink", website, RegistryValueKind.String);
                InstallInfo.SetValue("DisplayName", Resources.MenuTools, RegistryValueKind.String);
                InstallInfo.SetValue("DisplayVersion", GetAssembly.AssemblyInformation("version"), RegistryValueKind.String);
                /* User Settings */

                // All Files
                MenuToolsSettings.SetValue("OpenNotepadFiles", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("BlockWithFirewallFiles", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("CopyPathFiles", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("CopyNameFiles", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("AttributesFiles", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("SymlinkFiles", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("TakeOwnershipFiles", 0x00000001, RegistryValueKind.DWord);
                // All Files Shorcuts
                MenuToolsSettings.SetValue("OpenNotepadShort", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("CopyPathShortFiles", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("CopyNameShortFiles", 0x00000001, RegistryValueKind.DWord);
                // Directories
                MenuToolsSettings.SetValue("BlockWithFirewallDirectory", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("CopyPathDirectory", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("CopyNameDirectory", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("AttributesDirectory", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("SymlinkDirectory", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("TakeOwnershipDirectory", 0x00000001, RegistryValueKind.DWord);
                // Directory Background
                MenuToolsSettings.SetValue("AttributesDirectoryBack", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("CommandLinesDirectoryBack", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("FindWallpaperDirectoryBack", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("SystemFoldersDirectoryBack", 0x00000001, RegistryValueKind.DWord);
                MenuToolsSettings.SetValue("PasteContentsDirectoryBack", 0x00000001, RegistryValueKind.DWord);

                // Create Shorcut in All Users Start Menu Programs
                StringBuilder allUserProfile = new StringBuilder(260);
                NativeMethods.SHGetSpecialFolderPath(IntPtr.Zero, allUserProfile, NativeMethods.CSIDL_COMMON_STARTMENU, false);

                string programs_path = Path.Combine(allUserProfile.ToString(), "Programs");

                string shortcutFolder = Path.Combine(programs_path, @"MenuTools");
                if (!Directory.Exists(shortcutFolder))
                {
                    Directory.CreateDirectory(shortcutFolder);
                }

                using (new Settings())
                {
                    new Settings().ShowDialog();
                }
                CreateShortcut(shortcutFolder, InstallInfo.ToString());
                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                MessageForm(ex.Message + Environment.NewLine + ex.StackTrace, Resources.MenuTools, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(0);
            }
        }
        private void DoWork(string operation)
        {
            TryAgain :;
            try
            {
                int dotCount = 0;
                FilesCount = 0;
                Thread thread = new Thread(() => FileCounter())
                {
                    IsBackground = true
                };
                thread.Start();
                progressBar1.Value = 0;
                current            = 0;

                if (operation == Resources.FirewallArgs && RootPath != null)
                {
                    while (CountingFilesOperation)
                    {
                        Thread.Sleep(500);
                        Console.WriteLine(Resources.CountingFiles);
                        dotCount++;
                        if (dotCount == 2)
                        {
                            label1.Text = Operation;
                        }
                        if (dotCount == 4)
                        {
                            label1.Text = Operation + ".";
                        }
                        if (dotCount == 6)
                        {
                            label1.Text = Operation + "..";
                        }
                        if (dotCount == 8)
                        {
                            label1.Text = Operation + "...";
                            dotCount    = 0;
                        }
                        Application.DoEvents();
                    }
                    label1.Text = Resources.Blocking + Resources.outbound;
                    foreach (string item in RootPath)
                    {
                        try
                        {
                            while (PauseOperation)
                            {
                                Thread.Sleep(500);
                            }
                            current++;
                            string path      = Path.GetDirectoryName(item);
                            string title     = Path.GetFileName(path) + " - " + Path.GetFileName(item);
                            string arguments = "advfirewall firewall add rule name=" + "\"" + MainFolderName + " - " + title + "\"" + " dir=out program=" + "\"" + item + "\"" + " action=block";
                            progressBar1.Value = (current * 100) / FilesCount;
                            try
                            {
                                this.Text = Resources.BlockingFilesTitle + progressBar1.Value + Resources.Percentage;
                            }
                            catch (ArgumentOutOfRangeException)
                            {
                                continue;
                            }
                            label2.Text = Path.GetFileName(item);
                            Application.DoEvents();
                            StartProcess.StartInfo("netsh.exe", arguments, true, true, true);
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            continue;
                        }
                    }
                    current            = 0;
                    progressBar1.Value = 0;
                    label1.Text        = Resources.Blocking + Resources.inbound;
                    foreach (string item in RootPath)
                    {
                        try
                        {
                            while (PauseOperation)
                            {
                                Thread.Sleep(500);
                            }
                            current++;
                            string path      = Path.GetDirectoryName(item);
                            string title     = Path.GetFileName(path) + " - " + Path.GetFileName(item);
                            string arguments = "advfirewall firewall add rule name=" + "\"" + MainFolderName + " - " + title + "\"" + " dir=in program=" + "\"" + item + "\"" + " action=block";
                            progressBar1.Value = (current * 100) / FilesCount;
                            try
                            {
                                this.Text = Resources.BlockingFilesTitle + progressBar1.Value + Resources.Percentage;
                            }
                            catch (ArgumentOutOfRangeException)
                            {
                                continue;
                            }
                            label2.Text = Path.GetFileName(item);
                            Application.DoEvents();
                            StartProcess.StartInfo("netsh.exe", arguments, true, true, true);
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            continue;
                        }
                    }
                    Thread.Sleep(1500);
                    if (current > 0 && NoMoreThreads)
                    {
                        Hide();

                        DialogResult results = MessageForm(Resources.DialogMessageBlockFolder, Resources.DialogTitleSuccess, MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
                        if (results == DialogResult.Yes)
                        {
                            StartProcess.StartInfo("wf.msc");
                        }
                    }
                    else if (current == 0)
                    {
                        Hide();

                        MessageForm(Resources.DialogMessageFail, Resources.DialogTitleFail, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                ThreadRunning = false;
                if (NoMoreThreads)
                {
                    ExplorerRefresh.RefreshWindowsExplorer();
                    Environment.Exit(0);
                }
            }
            catch (Exception ex)
            {
                var dialog = MessageBox.Show("Too many errors have occured. Please close any programs that may be locking the files in the queue or reboot Windows and try again." + Environment.NewLine + Environment.NewLine + "The exact error message is: " + ex.Message + Environment.NewLine + Environment.NewLine + "Would you like to continue anyway?", Resources.ErrorTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                if (dialog == DialogResult.Yes)
                {
                    goto TryAgain;
                }
                else if (dialog == DialogResult.No)
                {
                    Environment.Exit(0);
                }
            }
        }
        private void ExecuteCommands(string[] args)
        {
            try
            {
                if (args[1] == Resources.MakeLink)
                {
                    string[]      selectPaths  = args[0].Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    int           items        = 0;
                    string        selectedItem = null;
                    List <string> Paths        = new List <string>();
                    string        PathName     = null;
                    try
                    {
                        foreach (var selected in selectPaths)
                        {
                            selectedItem = selected;
                            items++;
                        }
                        if (items == 1)
                        {
                            using (new InputBox())
                                new InputBox(Path.GetFileName(selectedItem)).ShowDialog();
                            using (FolderBrowserDialog ofd = new FolderBrowserDialog())
                            {
                                ofd.Description = Path.GetFileName(selectedItem);
                                if (ofd.ShowDialog() == DialogResult.OK)
                                {
                                    PathName = ofd.SelectedPath + @"\" + JunctionName;
                                    StartProcess.StartInfo("cmd.exe", "/c mklink /J " + "\"" + PathName + "\"" + " " + "\"" + selectedItem + "\"", true, true, true);
                                    Paths.Add(PathName);
                                }
                            }
                            if (PathName != null)
                            {
                                StartProcess.StartInfo(PathName);
                            }
                        }
                        else
                        {
                            MessageForm("Please try again only selecting one directory at a time while creating junctions.", "MenuTools", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    catch (Win32Exception ex)
                    {
                        MessageForm(ex.Message + Environment.NewLine + ex.StackTrace, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                if (args[1] == Resources.CatchHandlerArgs)
                {
                    MessageForm(args[0], Resources.MenuTools, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (args[1] == Resources.AttributesMenuArgs)
                {
                    string[]       array = args[0].Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    AttributesMenu menu  = new AttributesMenu(array);
                    using (menu)
                    {
                        menu.ShowDialog();
                    }
                    Environment.Exit(0);
                }
                if (args[1] == Resources.FirewallFilesArgs)
                {
                    KeepAlive = true;
                    NoErrors  = true;
                    string[] array = args[0].Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                    Thread thread = new Thread(() => MultiSelectFirewallFiles(array))
                    {
                        IsBackground = true
                    };
                    thread.Start();
                }
                if (args[1] == Resources.FirewallFolderArgs)
                {
                    KeepAlive = true;
                    this.Text = Resources.BlockingFilesTitle;
                    Thread thread = new Thread(() => FireUpThread(args[0], Resources.FirewallArgs))
                    {
                        IsBackground = true
                    };
                    thread.Start();
                }
                if (args[1] == Resources.OwnershipArgs)
                {
                    string[] array = args[0].Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    foreach (string item in array)
                    {
                        if (File.Exists(item))
                        {
                            StartProcess.StartInfo("cmd.exe", "/c takeown /f " + "\"" + item + "\"" + " /SKIPSL && icacls " + "\"" + item + "\"" + " /grant:r " + "\"" + CurrentUser + "\"" + ":F /t /l /c /q", false, true);
                        }
                        if (Directory.Exists(item))
                        {
                            StartProcess.StartInfo("cmd.exe", "/c takeown /f " + "\"" + item + "\"" + " /r /SKIPSL /d y && icacls " + "\"" + item + "\"" + " /grant:r " + "\"" + CurrentUser + "\"" + ":F /t /l /c /q", false, true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageForm(ex.Message + Environment.NewLine + ex.StackTrace, Resources.MenuTools, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(0);
            }
        }