Exemple #1
0
        public void SaveSettings(Dictionary <string, string> regKeys, List <string> procKeys)
        {
            // Validate
            List <string> badKeys = regKeys.Select(entry => entry.Key).Where(key => !key.StartsWith("HKEY")).ToList();

            if (badKeys.Count > 0)
            {
                throw new InvalidOperationException("Unexpected registry keys " + string.Join(",", badKeys));
            }
            var           allProcesses = KeepProcess.ListFocusableProcesses();
            List <string> badProc      = procKeys.Where(proc => !allProcesses.Contains(proc)).ToList();

            if (badProc.Count > 0)
            {
                throw new InvalidOperationException("Unreachable process " + string.Join(",", badProc));
            }

            // Save
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Clear();

            foreach (var entry in regKeys)
            {
                // Add an Application Setting.
                config.AppSettings.Settings.Add(entry.Key, entry.Value);
            }
            if (procKeys.Count > 0)
            {
                String processes = "";
                foreach (var entry in procKeys)
                {
                    if (processes.Length > 0)
                    {
                        processes += ",";
                    }
                    processes += entry;
                }
                config.AppSettings.Settings.Add(PROCESSES, processes);
            }
            // Save the changes in App.config file.
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of a changed section.
            ConfigurationManager.RefreshSection("appSettings");

            ConfigurationChanged(this);
        }
Exemple #2
0
        public MyCustomApplicationContext()
        {
            bool awake;

            try
            {
                awake = Boolean.Parse(ConfigurationManager.AppSettings.Get(ConfigurationHandler.AWAKE));
            }
            catch (ArgumentNullException)
            {
                awake = false;
            }
            awakeMenu         = new MenuItem("Awake process", AwakeProcess);
            awakeMenu.Checked = awake;
            awakeMenu.Enabled = false;
            // Initialize Tray Icon
            trayIcon = new NotifyIcon()
            {
                Icon        = Resources.AppIcon,
                ContextMenu = new ContextMenu(new MenuItem[] {
                    new MenuItem("Configure", Configure),
                    new MenuItem("Apply registry now", Apply),
                    new MenuItem("Keep alive", KeepAlive),
                    awakeMenu,
                    new MenuItem("Exit", Exit)
                }),
                Visible = true,
                Text    = "Registry watcher"
            };
            trayIcon.DoubleClick += Configure;

            configurationHandler       = new ConfigurationHandler();
            regWatcher                 = new RegWatcher(configurationHandler);
            regWatcher.ChangeDetected += OnChanged;
            configuration              = new Configuration(configurationHandler);
            regWatcher.Start();
            keepAlive   = new KeepAlive();
            keepProcess = new KeepProcess(configurationHandler);
        }