コード例 #1
0
        private void Main_Load(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.UpdateSettings) // If true, then first launch of latest EZBlocker
            {
                Properties.Settings.Default.Upgrade();
                Properties.Settings.Default.UpdateSettings = false;
                Properties.Settings.Default.Save();
            }

            if (!File.Exists(spotifyPath))
            {
                if (MessageBox.Show(Properties.strings.SpotifyNotFoundMessageBox, "EZBlocker", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    try // Remove Windows Store version.
                    {
                        ProcessStartInfo startInfo = new ProcessStartInfo
                        {
                            FileName  = "powershell.exe",
                            Arguments = "-Command \"Get-AppxPackage *Spotify* | Remove-AppxPackage\""
                        };
                        var uninstall = Process.Start(startInfo);
                        uninstall.WaitForExit();
                    }
                    catch (Exception ex) {
                        Debug.WriteLine(ex);
                    }
                    Process.Start("https://download.scdn.co/SpotifySetup.exe");
                }
                Application.Exit();
                return;
            }

            // Patch Spotify
            patcher = new SpotifyPatcher();
            CheckPatch(true);

            // Start Spotify and give EZBlocker higher priority
            try
            {
                if (Properties.Settings.Default.StartSpotify && File.Exists(spotifyPath) && Process.GetProcessesByName("spotify").Length < 1)
                {
                    Process.Start(spotifyPath);
                }
                Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; // Windows throttles down when minimized to task tray, so make sure EZBlocker runs smoothly
            }
            catch (Exception) {}

            // Set up UI
            if (File.Exists(hostsPath))
            {
                string hostsFile = File.ReadAllText(hostsPath);
                BlockBannersCheckbox.Checked = adHosts.All(host => hostsFile.Contains("0.0.0.0 " + host));
            }
            RegistryKey startupKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            if (startupKey.GetValue("EZBlocker") != null)
            {
                if (startupKey.GetValue("EZBlocker").ToString() == "\"" + Application.ExecutablePath + "\"")
                {
                    StartupCheckbox.Checked = true;
                    this.WindowState        = FormWindowState.Minimized;
                }
                else // Reg value exists, but not in right path
                {
                    startupKey.DeleteValue("EZBlocker");
                }
            }
            SpotifyCheckbox.Checked = Properties.Settings.Default.StartSpotify;

            // Set up Analytics
            if (String.IsNullOrEmpty(Properties.Settings.Default.CID))
            {
                Properties.Settings.Default.CID = Analytics.GenerateCID();
                Properties.Settings.Default.Save();
            }
            a = new Analytics(Properties.Settings.Default.CID, Assembly.GetExecutingAssembly().GetName().Version.ToString());

            // Start Spotify hook
            hook = new SpotifyHook();

            // Start EZBlocker listener
            listener = new Listener();
            Task.Run(() => listener.Listen());

            MainTimer.Enabled = true;

            LogAction("/launch");

            Task.Run(() => CheckUpdate());
        }