private void MonitorKeyLoop(RegistryMonitorPath registryPath) { int result = Win32APIDefinitions.RegOpenKeyEx((IntPtr)registryPath.RegistryHive, registryPath.RegistryKeyPath, 0, Win32APIDefinitions.STANDARD_RIGHTS_READ | Win32APIDefinitions.KEY_QUERY_VALUE | Win32APIDefinitions.KEY_NOTIFY, out var registryKey); if (result != 0) { return; } try { AutoResetEvent _eventNotify = new AutoResetEvent(false); WaitHandle[] waitHandles = new WaitHandle[] { _eventNotify }; while (!_cancelToken.IsCancellationRequested) { List <RegistryChangedObject> cachedValues = GetNamesAndValuesOfRegistryKey(registryPath.RegistryHive, registryPath.RegistryKeyPath).ToList(); result = Win32APIDefinitions.RegNotifyChangeKeyValue(registryKey, true, ValueRegChangeNotifyFilter, _eventNotify.SafeWaitHandle, true); if (result != 0) { throw new Win32Exception(result); } if (WaitHandle.WaitAny(waitHandles) == 0) { List <RegistryChangedObject> currentValues = GetNamesAndValuesOfRegistryKey(registryPath.RegistryHive, registryPath.RegistryKeyPath).ToList(); List <RegistryChangedObject> CachedWithCurrentDiff = currentValues.Except(cachedValues).ToList(); cachedValues = currentValues; ValueChanged?.Invoke(new ChangedValueInfo { MonitorPath = registryPath, ChangedObject = CachedWithCurrentDiff.FirstOrDefault() }); } } } finally { if (registryKey != IntPtr.Zero) { Win32APIDefinitions.RegCloseKey(registryKey); } } }
public string GetDriveMappingPath(string driveLetter) { if (string.IsNullOrEmpty(driveLetter)) { throw new ArgumentNullException(nameof(driveLetter)); } string normalizedDriveLetter = NormalizeDriveLetter(driveLetter); // Return empty if any error occured var sb = new StringBuilder(259); if (Win32APIDefinitions.QueryDosDevice(normalizedDriveLetter, sb, sb.Capacity) == 0) { return(string.Empty); } return(sb.ToString().Replace(Resources.DriveMapMustEscapeChars, string.Empty)); }