コード例 #1
0
        /// <summary>
        /// Refreshes the list of hosts files in the tray menu
        /// </summary>
        private void BuildMenu()
        {
            trayMenu.MenuItems.Clear();
            ///  trayMenu.MenuItems.Add(hostsPath);
            ///
            trayMenu.MenuItems.Add("Autostart", (o, e) =>
            {

                RegistryKey autostart = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",true);
              var  test = (string)autostart.GetValue("Hosts-Manager","");

                if (test == Application.ExecutablePath)
                {
                    autostart.DeleteValue("Hosts-Manager");
                    trayMenu.MenuItems[0].Checked = false;

                }
                else

                {

                    autostart.SetValue("Hosts-Manager", Application.ExecutablePath);

                    trayMenu.MenuItems[0].Checked = true;
                }

                autostart.Close();

            });

            trayMenu.MenuItems.Add("Hosts Datei schützen", (o, e) =>

            {

                if (trayMenu.MenuItems[1].Checked == true)
                {
                    trayMenu.MenuItems[1].Checked = false;
                    hostswatcher.EnableRaisingEvents=false;

                }
            else
                {
                    trayMenu.MenuItems[1].Checked = true;
                    hostswatcher.EnableRaisingEvents = true;

                }

            });
            trayMenu.MenuItems.Add("-");
            foreach (var file in new DirectoryInfo(".").GetFiles())
            {
                if (file.Extension == extension)
                {
                    trayMenu.MenuItems.Add(file.Name.Remove(file.Name.LastIndexOf('.')), MenuChanged);
                    trayMenu.MenuItems[trayMenu.MenuItems.Count - 1].Checked = trayMenu.MenuItems[trayMenu.MenuItems.Count - 1].Text == currentHosts;
                }
            }
            trayMenu.MenuItems.Add("-");

            trayMenu.MenuItems.Add("Download Malwaredomain Liste", (o, e) =>
            {
                if (File.Exists("Malware Domains.hosts"))
                { File.Delete("Malware Domains.hosts"); }

                    WebClient Client = new WebClient();
                Client.DownloadFile("http://www.malwaredomainlist.com/hostslist/hosts.txt", @"Malware Domains.hosts");
            });

            trayMenu.MenuItems.Add("Neu", (o, e) =>
            {
                var newFrm = new FrmNew();
                if (newFrm.ShowDialog() == DialogResult.OK)
                {
                    if (!File.Exists(newFrm.HostsName))
                    {
                        File.WriteAllText(newFrm.HostsName, File.Exists("Default" + extension) ? File.ReadAllText("Default" + extension) : String.Empty);
                        DropPermissions(newFrm.HostsName);
                    }
                    EditFile(newFrm.HostsName);
                }
            });

            trayMenu.MenuItems.Add("Bearbeiten/Löschen", (o, e) =>
            {
                var editFrm = new FrmEdit(currentHosts);
                if (editFrm.ShowDialog() == DialogResult.OK)
                {
                    if (editFrm.Action == FileAction.Edit)
                    {
                        EditFile(editFrm.HostsName);
                    }
                    else
                    {
                        File.Delete(editFrm.HostsName);
                        if (editFrm.HostsName.Remove(editFrm.HostsName.LastIndexOf('.')) == currentHosts)
                        {
                            currentHosts = "\t"; //It should be impossible for a hosts file to be named this, so this is a dandy null value
                        }
                    }
                }
            });
            trayMenu.MenuItems[2].Enabled = trayMenu.MenuItems.Count != 3; //Disable Edit/Delete if there are no hosts files
            trayMenu.MenuItems.Add("Exit", (o, e) =>
            {
                RegistryKey lastHosts = Registry.LocalMachine.CreateSubKey("SOFTWARE\\HostsManager");
                lastHosts.SetValue("last", currentHosts);
                lastHosts.Close();
                Application.Exit();
            });
        }
コード例 #2
0
        /// <summary>
        /// Refreshes the list of hosts files in the tray menu
        /// </summary>
        private void BuildMenu()
        {
            trayMenu.MenuItems.Clear();
            ///  trayMenu.MenuItems.Add(hostsPath);
            ///
            trayMenu.MenuItems.Add("Autostart", (o, e) =>
            {
                RegistryKey autostart = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                var test = (string)autostart.GetValue("Hosts-Manager", "");



                if (test == Application.ExecutablePath)
                {
                    autostart.DeleteValue("Hosts-Manager");
                    trayMenu.MenuItems[0].Checked = false;
                }
                else

                {
                    autostart.SetValue("Hosts-Manager", Application.ExecutablePath);

                    trayMenu.MenuItems[0].Checked = true;
                }



                autostart.Close();
            });

            trayMenu.MenuItems.Add("Hosts Datei schützen", (o, e) =>

            {
                if (trayMenu.MenuItems[1].Checked == true)
                {
                    trayMenu.MenuItems[1].Checked    = false;
                    hostswatcher.EnableRaisingEvents = false;
                }
                else
                {
                    trayMenu.MenuItems[1].Checked    = true;
                    hostswatcher.EnableRaisingEvents = true;
                }
            });
            trayMenu.MenuItems.Add("-");
            foreach (var file in new DirectoryInfo(".").GetFiles())
            {
                if (file.Extension == extension)
                {
                    trayMenu.MenuItems.Add(file.Name.Remove(file.Name.LastIndexOf('.')), MenuChanged);
                    trayMenu.MenuItems[trayMenu.MenuItems.Count - 1].Checked = trayMenu.MenuItems[trayMenu.MenuItems.Count - 1].Text == currentHosts;
                }
            }
            trayMenu.MenuItems.Add("-");

            trayMenu.MenuItems.Add("Download Malwaredomain Liste", (o, e) =>
            {
                if (File.Exists("Malware Domains.hosts"))
                {
                    File.Delete("Malware Domains.hosts");
                }

                WebClient Client = new WebClient();
                Client.DownloadFile("http://www.malwaredomainlist.com/hostslist/hosts.txt", @"Malware Domains.hosts");
            });

            trayMenu.MenuItems.Add("Neu", (o, e) =>
            {
                var newFrm = new FrmNew();
                if (newFrm.ShowDialog() == DialogResult.OK)
                {
                    if (!File.Exists(newFrm.HostsName))
                    {
                        File.WriteAllText(newFrm.HostsName, File.Exists("Default" + extension) ? File.ReadAllText("Default" + extension) : String.Empty);
                        DropPermissions(newFrm.HostsName);
                    }
                    EditFile(newFrm.HostsName);
                }
            });


            trayMenu.MenuItems.Add("Bearbeiten/Löschen", (o, e) =>
            {
                var editFrm = new FrmEdit(currentHosts);
                if (editFrm.ShowDialog() == DialogResult.OK)
                {
                    if (editFrm.Action == FileAction.Edit)
                    {
                        EditFile(editFrm.HostsName);
                    }
                    else
                    {
                        File.Delete(editFrm.HostsName);
                        if (editFrm.HostsName.Remove(editFrm.HostsName.LastIndexOf('.')) == currentHosts)
                        {
                            currentHosts = "\t"; //It should be impossible for a hosts file to be named this, so this is a dandy null value
                        }
                    }
                }
            });
            trayMenu.MenuItems[2].Enabled = trayMenu.MenuItems.Count != 3; //Disable Edit/Delete if there are no hosts files
            trayMenu.MenuItems.Add("Exit", (o, e) =>
            {
                RegistryKey lastHosts = Registry.LocalMachine.CreateSubKey("SOFTWARE\\HostsManager");
                lastHosts.SetValue("last", currentHosts);
                lastHosts.Close();
                Application.Exit();
            });
        }