Close Registry Keys After Use


To open a windows registry key using C#, the code would look something like this:

RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\TWC\\Applications", true);

The RegistryKey object has lots of methods on it to CreateSubKey, DeleteSubKey, SetValue, DeleteValue, etc. Once you are done with the registry key, you MUST close the registry key by calling the Close() method. If you don’t close the registry key, you will leak registry connections (and memory). There is a limit of 32756 connections that one application can make. If you don’t close the connections, then you can eventually run out of them.

Not closing connections is particular bad when you are looping through thousands of records and accessing the registry every time.

Comments (0)

Skip to main content