Exemplo n.º 1
0
 private ThemeManager()
 {
     themeWatcher = new RegistryWatcher <int>(ThemeRegistryPath, ThemeRegistryKey, GetDefaultKeys());
     themeWatcher.ValueChanged += ThemeChangedHandler;
     themeWatcher.Fire();
     themeWatcher.Start();
 }
Exemplo n.º 2
0
        private void InitializeDefaultInterpreterWatcher(IServiceProvider serviceProvider)
        {
            RegistryKey userSettingsKey;

            if (serviceProvider != null)
            {
                userSettingsKey = VSRegistry.RegistryRoot(serviceProvider, __VsLocalRegistryType.RegType_UserSettings, false);
            }
            else
            {
                userSettingsKey = VSRegistry.RegistryRoot(__VsLocalRegistryType.RegType_UserSettings, false);
            }
            using (userSettingsKey) {
                RegistryHive hive;
                RegistryView view;
                string       keyName;
                if (RegistryWatcher.GetRegistryKeyLocation(userSettingsKey, out hive, out view, out keyName))
                {
                    try {
                        RegistryWatcher.Instance.Add(hive, view, keyName + "\\" + DefaultInterpreterOptionsCollection,
                                                     DefaultInterpreterRegistry_Changed,
                                                     recursive: false, notifyValueChange: true, notifyKeyChange: false);
                    } catch (ArgumentException) {
                        // DefaultInterpreterOptions subkey does not exist yet, so
                        // create it and then start the watcher.
                        SaveDefaultInterpreter();

                        RegistryWatcher.Instance.Add(hive, view, keyName + "\\" + DefaultInterpreterOptionsCollection,
                                                     DefaultInterpreterRegistry_Changed,
                                                     recursive: false, notifyValueChange: true, notifyKeyChange: false);
                    }
                }
            }
        }
Exemplo n.º 3
0
 private ThemeManager()
 {
     // The ThemeWatcher will have no default keys.
     themeWatcher = new RegistryWatcher <int>(ThemeRegistryPath, ThemeRegistryKey, new List <int>());
     themeWatcher.ValueChanged += ThemeChangedHandler;
     themeWatcher.Fire();
     themeWatcher.Start();
 }
Exemplo n.º 4
0
        public void RegistryWatcherDeleteRecursive()
        {
            string keyName = "RegistryWatcherDeleteRecursive";

            SetValue(keyName, "TestValue1", "ABC");
            SetValue(keyName, "TestValue2", "DEF");
            SetValue(keyName, "TestValue3", "GHI");
            SetValue(keyName + "\\TestKey1", "Value", 123);
            SetValue(keyName + "\\TestKey2", "Value", 456);

            using (var watcher = new RegistryWatcher())
            {
                RegistryChangedEventArgs args = null;
                var argsSet = new ManualResetEventSlim();

                var watch1 = AddWatch(watcher, keyName,
                                      e => { args = e; argsSet.Set(); },
                                      recursive: true);

                // Value is deleted
                DeleteValue(keyName, "TestValue2");
                Assert.IsTrue(argsSet.Wait(TIMEOUT));
                Assert.IsNotNull(args);
                Assert.AreEqual(GetKey(keyName), args.Key);
                argsSet.Reset();
                args = null;

                // Value in subkey is deleted
                DeleteValue(keyName + "\\TestKey1", "Value");
                Assert.IsTrue(argsSet.Wait(TIMEOUT));
                Assert.IsNotNull(args);
                Assert.AreEqual(GetKey(keyName), args.Key);
                argsSet.Reset();
                args = null;

                // Subkey is deleted
                DeleteKey(keyName + "\\TestKey1");
                Assert.IsTrue(argsSet.Wait(TIMEOUT));
                Assert.IsNotNull(args);
                Assert.AreEqual(GetKey(keyName), args.Key);
                argsSet.Reset();
                args = null;

                watcher.Remove(watch1);

                // Another value is deleted, but we don't get notified
                DeleteValue(keyName, "TestValue3");
                Assert.IsFalse(argsSet.Wait(TIMEOUT));
                Assert.IsNull(args);

                // Another key is deleted, but we don't get notified
                DeleteKey(keyName + "\\TestKey2");
                Assert.IsFalse(argsSet.Wait(TIMEOUT));
                Assert.IsNull(args);
            }
        }
        internal static RegistryWatcher CreateRegistryWatcher()
        {
            RegistryWatcher watcher = null;

            new AlternateServiceAccountConfiguration(null).DoRegistryOperation(false, delegate(RegistryKey key)
            {
                watcher = new RegistryWatcher(AlternateServiceAccountConfiguration.RootRegistryKeyName, true);
            }, new Func <string, LocalizedString>(DirectoryStrings.FailedToReadAlternateServiceAccountConfigFromRegistry));
            return(watcher);
        }
Exemplo n.º 6
0
        public void RegistryWatcher100Keys()
        {
            string keyName = "RegistryWatcher100Keys";

            for (int i = 0; i < 100; ++i)
            {
                SetValue(string.Format("{0}\\Key{1}", keyName, i), "Value", "ABC");
            }

            using (var watcher = new RegistryWatcher())
            {
                RegistryChangedEventArgs[] args = new RegistryChangedEventArgs[100];
                var argsSet = args.Select(_ => new ManualResetEventSlim()).ToArray();
                global::System.Object[] tokens = new object[100];

                for (int i = 0; i < 100; ++i)
                {
                    tokens[i] = AddWatch(watcher, string.Format("{0}\\Key{1}", keyName, i),
                                         new ArgSetter(args, argsSet, i).Raised);
                }

                // Change the first value
                SetValue(keyName + "\\Key0", "Value", "DEF");
                Assert.IsTrue(argsSet[0].Wait(TIMEOUT));
                Assert.IsNotNull(args[0]);
                Assert.AreEqual(GetKey(keyName + "\\Key0"), args[0].Key);
                argsSet[0].Reset();
                args[0] = null;

                // Change the last value
                SetValue(keyName + "\\Key99", "Value", "DEF");
                Assert.IsTrue(argsSet[99].Wait(TIMEOUT));
                Assert.IsNotNull(args[99]);
                Assert.AreEqual(GetKey(keyName + "\\Key99"), args[99].Key);
                argsSet[99].Reset();
                args[99] = null;

                watcher.Remove(tokens[0]);
                watcher.Remove(tokens[99]);

                // Change the first value
                SetValue(keyName + "\\Key0", "Value", "GHI");
                Assert.IsFalse(argsSet[0].Wait(TIMEOUT));
                Assert.IsNull(args[0]);

                // Change the last value
                SetValue(keyName + "\\Key99", "Value", "GHI");
                Assert.IsFalse(argsSet[99].Wait(TIMEOUT));
                Assert.IsNull(args[99]);
            }
        }
Exemplo n.º 7
0
        public void RegistryWatcherUpdateRecursive()
        {
            string keyName = "RegistryWatcherUpdateRecursive";

            SetValue(keyName, "TestValue", "ABC");
            SetValue(keyName + "\\TestKey", "Value", 123);

            using (var watcher = new RegistryWatcher())
            {
                RegistryChangedEventArgs args = null;
                var argsSet = new ManualResetEventSlim();

                var watch1 = AddWatch(watcher, keyName,
                                      e => { args = e; argsSet.Set(); },
                                      recursive: true);

                // Value is set, but does not actually change
                SetValue(keyName, "TestValue", "ABC");
                Assert.IsFalse(argsSet.Wait(TIMEOUT));
                Assert.IsNull(args);

                // Value is changed
                SetValue(keyName, "TestValue", "DEF");
                Assert.IsTrue(argsSet.Wait(TIMEOUT));
                Assert.IsNotNull(args);
                Assert.AreEqual(GetKey(keyName), args.Key);
                argsSet.Reset();
                args = null;

                // Value in subkey is changed
                SetValue(keyName + "\\TestKey", "Value", 456);
                Assert.IsTrue(argsSet.Wait(TIMEOUT));
                Assert.IsNotNull(args);
                Assert.AreEqual(GetKey(keyName), args.Key);
                argsSet.Reset();
                args = null;

                watcher.Remove(watch1);

                // Value is changed back, but we don't get notified
                SetValue(keyName, "TestValue", "ABC");
                Assert.IsFalse(argsSet.Wait(TIMEOUT));
                Assert.IsNull(args);

                // Value in subkey is changed back, but we don't notice
                SetValue(keyName + "\\TestKey", "Value", 123);
                Assert.IsFalse(argsSet.Wait(TIMEOUT));
                Assert.IsNull(args);
            }
        }
Exemplo n.º 8
0
 object AddWatch(RegistryWatcher watcher,
                 string subkey,
                 Action <RegistryChangedEventArgs> callback,
                 bool recursive         = false,
                 bool notifyValueChange = true,
                 bool notifyKeyChange   = true)
 {
     return(watcher.Add(
                RegistryHive.CurrentUser,
                RegistryView.Default,
                GetKey(subkey),
                (s, e) => { callback(e); },
                recursive,
                notifyValueChange,
                notifyKeyChange));
 }
Exemplo n.º 9
0
        private static void Main(string[] args)
        {
            var thisProc = Process.GetCurrentProcess();

            _appPath = thisProc.MainModule.FileName;
            _appArgs = args.Select(s => s.TrimStart('/', '-').ToLower().Trim()).ToArray();

            Application.ThreadException += Application_ThreadException;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            // ensure we can start this instance
            if (!CanStartThisInstance())
            {
                MessageBox.Show("Another instance of " + Program.Name + " is already running.\n\n" +
                                "Only one instance can run at a time.", "Previous Instance Detected",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return; // terminate program execution
            }


            // prompt to enable auto-start on boot if the noAutorun flag is absent (main process only)
            if (!IsChildProcess && !Program.Arguments.Contains(ValidArgs.NoAutorun))
            {
                SetAutoStart();
            }


            /*  Start child process in the following conditions
             *    1. This is not a child process
             *    2. There is no debugger attached
             *    3. The singleProcess argument was not passed
             *    4. Parent process exited before this one is completely initialized
             */
            if (CanStartChildProcess())
            {
                childExitTimes = new List <DateTime>();
                // recursively start new child processes if exit code is not 0 (successful exit)
                do
                {
                    var childProcArgs = ValidArgs.ParentProcId + thisProc.Id.ToString();
                    _childProc = Process.Start(AppPath.Replace(".vshost.exe", ".exe"), childProcArgs);

                    _childProc.WaitForExit();

                    System.Threading.Thread.Sleep(1000);
                } while (ShouldRestartChildProc(_childProc.ExitCode));

                return; // terminate program execution
            }


            //!+ Only child process can get here.

            // add event handler to exit this instance when the parent process exits
            if (ParentProcess != null && !ParentProcess.HasExited)
            {
                ParentProcess.EnableRaisingEvents = true;
                ParentProcess.Exited += ParentProcess_Exited;
            }


            // initialize data directories and data files
            var netConfigRegPath = @"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings";

            _appDataPath = GetAppDataPath();
            LoadNetworkSettings();

            // initialize the network and registry watchers
            _netWatcher = new NetworkWatcher();
            _regWatcher = new RegistryUtils.RegistryMonitor(netConfigRegPath);
            _regWatcher.RegChangeNotifyFilter = RegistryUtils.RegChangeNotifyFilter.Value;


            Application.Run(new frmMain());


            // stop watchers
            NetworkWatcher.Stop();
            RegistryWatcher.Stop();
            SaveNetworkSettings(); // save settings before exiting
        }
Exemplo n.º 10
0
        public void Config(BeSafeConfig config, PipeServer pipeServer, bool stoppingService)
        {
            _config     = config;
            _pipeServer = pipeServer;

            bool stateResult;

            if ((config?.ComponentsState.RegistryWatcher == true) && (stoppingService == false))
            {
                string userSID = config.UserSID;

                _registryWatcher = new RegistryWatcher(new List <RegistryMonitorPath>
                {
                    // CurrentUser keys
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.Users, RegistryKeyPath = $@"{userSID}\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.Users, RegistryKeyPath = $@"{userSID}\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.Users, RegistryKeyPath = $@"{userSID}\Software\Microsoft\Windows\CurrentVersion\RunServices"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.Users, RegistryKeyPath = $@"{userSID}\Software\Microsoft\Windows\CurrentVersion\Run"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.Users, RegistryKeyPath = $@"{userSID}\Software\Microsoft\Windows\CurrentVersion\RunOnce"
                    },

                    // LocalMachine keys
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\explorer\Shell Folders"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\explorer\User Shell Folders"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\RunServices"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\RunServicesOnce"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Run"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\RunOnce"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"SOFTWARE\Classes\cplfile\shell\cplopen\command"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"SOFTWARE\Classes\batfile\shell\open\command"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"Software\CLASSES\comfile\shell\open\command"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"Software\CLASSES\exefile\shell\open\command"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"Software\CLASSES\htafile\Shell\Open\Command"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"Software\CLASSES\piffile\shell\open\command"
                    },
                    new RegistryMonitorPath {
                        RegistryHive = RegistryHive.LocalMachine, RegistryKeyPath = @"SOFTWARE\Classes\scrfile\shell\open\command"
                    },
                });

                _registryWatcher.ValueChanged += ValueChangedArrived;
                stateResult = _registryWatcher.Start();

                Task.Run(() => StackScanner(_changedValuesStack));
                return;
            }

            stateResult = _registryWatcher?.Stop() ?? false;
        }