.NET 3.5 Registry Key Utilization

I have the following code:

RegistryKey installKey = Registry.LocalMachine.OpenSubKey(installKey); 

I am running a static analysis tool on my code, and this gives me a flaw saying that I am returning from meditation without having to use installKey . I know that you can call Dispose () on RegistryKey in .NET 4.0 or later, but my code works on .NET 3.5.

Does anyone know a better way to Dispose this RegistryKey and keep my static analysis tool happy?

+4
source share
2 answers

You must wrap your code in a using block, which indirectly calls Dispose for you. It is not clear which static analysis tool you are using, but hopefully it understands using :

 using (RegistryKey installKey = Registry.LocalMachine.OpenSubKey(installKey)) { // Your code here } 

Note that you can also explicitly call Dispose , but you must first drop RegistryKey to IDisposable :

 ((IDisposable)installKey).Dispose() 
+8
source

Of course, it can be used in version 3.5! See the documentation here .

Use the using block, as in the MSDN example here , or simply call Dispose (), as in any other IDisposable.

+2
source

Source: https://habr.com/ru/post/1403845/


All Articles