/// <summary> /// Check whether the key to be monitored exists, and then /// start ManagementEventWatcher to watch the RegistryKeyChangeEvent /// </summary> /// <returns>True if the ManagementEventWatcher starts successfully.</returns> bool StartMonitor() { RegistryKey hive = cmbHives.SelectedValue as RegistryKey; var keyPath = tbRegkeyPath.Text.Trim(); try { watcher = new RegistryWatcher(hive, keyPath); } // The constructor of RegistryWatcher may throw a SecurityException when // the key to monitor does not exist. catch (System.ArgumentException ex) { MessageBox.Show(ex.Message); return(false); } // The constructor of RegistryWatcher may throw a SecurityException when // current user does not have the permission to access the key to monitor. catch (System.Security.SecurityException) { string message = string.Format( @"You do not have permission to access the key {0}\{1}.", hive.Name, keyPath); MessageBox.Show(message); return(false); } try { // Set up the handler that will handle the change event. watcher.RegistryKeyChangeEvent += new EventHandler <RegistryKeyChangeEventArgs>( watcher_RegistryKeyChangeEvent); // Start listening for events. watcher.Start(); return(true); } catch (System.Runtime.InteropServices.COMException comException) { MessageBox.Show("An error occurred: " + comException.Message); return(false); } catch (ManagementException managementException) { MessageBox.Show("An error occurred: " + managementException.Message); return(false); } }
/// <summary> /// 检查被监控的项是否存在,然后启动ManagementEventWatcher来监控 /// RegistryKeyChangeEvent事件。 /// </summary> /// <returns>如果ManagementEventWatcher启动成功则为真。</returns> bool StartMonitor() { RegistryKey hive = cmbHives.SelectedValue as RegistryKey; var keyPath = tbRegkeyPath.Text.Trim(); try { watcher = new RegistryWatcher(hive, keyPath); } // 当被监控的项不存在时,RegistryWatcher的构造器可能会抛出一个SecurityException异常。 catch (System.ArgumentException ex) { MessageBox.Show(ex.Message); return(false); } // 当前用户没有访问被监控项的权限时,RegistryWatcher的构造器可能会抛出一个 // SecurityException异常。 catch (System.Security.SecurityException) { string message = string.Format( @"您没有访问项 {0}\{1} 的权限。", hive.Name, keyPath); MessageBox.Show(message); return(false); } try { // 设置用于处理变更事件的句柄。 watcher.RegistryKeyChangeEvent += new EventHandler <RegistryKeyChangeEventArgs>( watcher_RegistryKeyChangeEvent); // 启动监听事件。 watcher.Start(); return(true); } catch (System.Runtime.InteropServices.COMException comException) { MessageBox.Show("发生错误: " + comException.Message); return(false); } catch (ManagementException managementException) { MessageBox.Show("发生错误: " + managementException.Message); return(false); } }
/// <summary> /// 检查被监控的项是否存在,然后启动ManagementEventWatcher来监控 /// RegistryKeyChangeEvent事件。 /// </summary> /// <returns>如果ManagementEventWatcher启动成功则为真。</returns> bool StartMonitor() { RegistryKey hive = cmbHives.SelectedValue as RegistryKey; var keyPath = tbRegkeyPath.Text.Trim(); try { watcher = new RegistryWatcher(hive, keyPath); } // 当被监控的项不存在时,RegistryWatcher的构造器可能会抛出一个SecurityException异常。 catch (System.ArgumentException ex) { MessageBox.Show(ex.Message); return false; } // 当前用户没有访问被监控项的权限时,RegistryWatcher的构造器可能会抛出一个 // SecurityException异常。 catch (System.Security.SecurityException) { string message = string.Format( @"您没有访问项 {0}\{1} 的权限。", hive.Name, keyPath); MessageBox.Show(message); return false; } try { // 设置用于处理变更事件的句柄。 watcher.RegistryKeyChangeEvent += new EventHandler<RegistryKeyChangeEventArgs>( watcher_RegistryKeyChangeEvent); // 启动监听事件。 watcher.Start(); return true; } catch (System.Runtime.InteropServices.COMException comException) { MessageBox.Show("发生错误: " + comException.Message); return false; } catch (ManagementException managementException) { MessageBox.Show("发生错误: " + managementException.Message); return false; } }