public void Dispose() { if (_hkey != null) { if (!IsSystemKey()) { try { _hkey.Dispose(); } catch (IOException) { // we don't really care if the handle is invalid at this point } finally { _hkey = null; } } else if (IsPerfDataKey()) { ClosePerfDataKey(); } } }
void IDisposable.Dispose() { if (_hkey != null) { _hkey.Dispose(); } }
private void Dispose(bool disposing) { if (hkey != null) { if (!IsSystemKey()) { try { hkey.Dispose(); } catch (IOException) { // we don't really care if the handle is invalid at this point } finally { hkey = null; } } else if (disposing && IsPerfDataKey()) { // System keys should never be closed. However, we want to call RegCloseKey // on HKEY_PERFORMANCE_DATA when called from PerformanceCounter.CloseSharedResources // (i.e. when disposing is true) so that we release the PERFLIB cache and cause it // to be refreshed (by re-reading the registry) when accessed subsequently. // This is the only way we can see the just installed perf counter. // NOTE: since HKEY_PERFORMANCE_DATA is process wide, there is inherent race condition in closing // the key asynchronously. While Vista is smart enough to rebuild the PERFLIB resources // in this situation the down level OSes are not. We have a small window between // the dispose below and usage elsewhere (other threads). This is By Design. // This is less of an issue when OS > NT5 (i.e Vista & higher), we can close the perfkey // (to release & refresh PERFLIB resources) and the OS will rebuild PERFLIB as necessary. SafeRegistryHandle.RegCloseKey(RegistryKey.HKEY_PERFORMANCE_DATA); } } }
public void Dispose() { if (userProfile != null && !userProfile.IsClosed) { try { Unload(); } catch { // Don't throw in dispose method } userProfile.Dispose(); } }
protected void Dispose(bool disposing) { // Ordinarily, we release unmanaged resources here; // but all are wrapped by safe handles. // Release disposable objects. if (disposing) { if (hExtHandle != null) { hExtHandle.Dispose(); } if (hAppIdHandle != null) { hAppIdHandle.Dispose(); } } }
private void Dispose(bool disposing) { if (hkey != null) { if (!IsSystemKey()) { try { hkey.Dispose(); } catch (IOException) { // we don't really care if the handle is invalid at this point } finally { hkey = null; } } } }
// protected virtual void Dispose(bool disposing) { if (_disposed == true) { return; } if (disposing == true) { // dispose of our underlying registry key's access handle _handle.Dispose(); } _disposed = true; // trigger the notification pool to remove our entry (if we were subscribed) try { // TODO: consider having a "SubscribedToNotifications" flag in the RegistryKey (and using that flag to determine if this is necessary/appropriate) s_registryKeyNotifyPoolUpdatedEvent.Set(); } catch { } }
internal static RegistryDataKey Open(string registryPath, bool fCreateIfNotExist) { // Sanity check if (string.IsNullOrEmpty(registryPath)) { return(null); } // If the last character is a '\', get rid of it registryPath = registryPath.Trim(new char[] { '\\' }); string rootPath = GetFirstKeyAndParseRemainder(ref registryPath); // Get the native registry handle and subkey path SafeRegistryHandle regHandle = RootHKEYFromRegPath(rootPath); // If there's no root, we can't do anything. if (regHandle == null || regHandle.IsInvalid) { regHandle?.Dispose(); return(null); } RegistryDataKey rootKey = new(rootPath, regHandle); // If the path was only a root, we can directly return the key; otherwise, // we need to open a subkey and return that. if (string.IsNullOrEmpty(registryPath)) { return(rootKey); } else { RegistryDataKey subKey = OpenSubKey(rootKey, registryPath, fCreateIfNotExist); return(subKey); } }
public void Dispose() { _hkey.Dispose(); }
public FileAssociationInfo(String fileExtension) { int retVal = 0; uint lpType = 0; if (!fileExtension.StartsWith(".")) { fileExtension = "." + fileExtension; } ext = fileExtension; IntPtr hExtension = IntPtr.Zero; // Get the file extension value. retVal = RegOpenKeyEx(new IntPtr(HKEY_CLASSES_ROOT), fileExtension, 0, KEY_QUERY_VALUE, out hExtension); if (retVal != ERROR_SUCCESS) { throw new Win32Exception(retVal); } // Instantiate the first SafeRegistryHandle. hExtHandle = new SafeRegistryHandle(hExtension, true); string appId = new string(' ', MAX_PATH); uint appIdLength = (uint)appId.Length; retVal = RegQueryValueEx(hExtHandle.DangerousGetHandle(), String.Empty, 0, out lpType, appId, ref appIdLength); if (retVal != ERROR_SUCCESS) { throw new Win32Exception(retVal); } // We no longer need the hExtension handle. hExtHandle.Dispose(); // Determine the number of characters without the terminating null. appId = appId.Substring(0, (int)appIdLength / 2 - 1) + @"\shell\open\Command"; // Open the application identifier key. string exeName = new string(' ', MAX_PATH); uint exeNameLength = (uint)exeName.Length; IntPtr hAppId; retVal = RegOpenKeyEx(new IntPtr(HKEY_CLASSES_ROOT), appId, 0, KEY_QUERY_VALUE | KEY_SET_VALUE, out hAppId); if (retVal != ERROR_SUCCESS) { throw new Win32Exception(retVal); } // Instantiate the second SafeRegistryHandle. hAppIdHandle = new SafeRegistryHandle(hAppId, true); // Get the executable name for this file type. string exePath = new string(' ', MAX_PATH); uint exePathLength = (uint)exePath.Length; retVal = RegQueryValueEx(hAppIdHandle.DangerousGetHandle(), String.Empty, 0, out lpType, exePath, ref exePathLength); if (retVal != ERROR_SUCCESS) { throw new Win32Exception(retVal); } // Determine the number of characters without the terminating null. exePath = exePath.Substring(0, (int)exePathLength / 2 - 1); // Remove any environment strings. exePath = Environment.ExpandEnvironmentVariables(exePath); int position = exePath.IndexOf('%'); if (position >= 0) { args = exePath.Substring(position); // Remove command line parameters ('%0', etc.). exePath = exePath.Substring(0, position).Trim(); } openCmd = exePath; }
void IDisposable.Dispose() { _hkey?.Dispose(); }