Click here to Skip to main content
12,882,820 members (37,298 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0

I just wondering if i need to close the registry if use any of the following (OpenRemoteBaseKey,OpenSubKey, etc,etc) see code below ? Thanks for your help.
//*************BEGIN GENERAL TAB***********************************
            //Select the RegistryHive you wish to read from 
            RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, "");
            //Select the path within the hive 
            RegistryKey subkey1 = key.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
            string[] names1 = subkey1.GetValueNames();
            string YesSyncMode5NameExists = "";
            string NoSyncMode5NameExists = "";
            foreach (string name in names1)
                if (name.Equals("SyncMode5", StringComparison.CurrentCulture))
                    // Found SyncMode5
                    YesSyncMode5NameExists = "1";
                    //NoSyncMode5NameExists = "99";
                    // No Found SyncMode5
                    NoSyncMode5NameExists = "1";
                    //subkey1.SetValue("SyncMode5", "2", RegistryValueKind.DWord);
            if (YesSyncMode5NameExists == "1")
                Console.WriteLine("YES Found SyncMode5");
                int myValue = (int)subkey1.GetValue("SyncMode5");
                if (myValue != 2)
                    //Console.WriteLine("Setting the right GE Recommendation");
                    subkey1.SetValue("SyncMode5", "2", RegistryValueKind.DWord);
            if (NoSyncMode5NameExists == "1")
                //Console.WriteLine("NO Found SyncMode5, creating it ");
                //Console.WriteLine("Setting the right GE Recommendation");
                subkey1.SetValue("SyncMode5", "2", RegistryValueKind.DWord);
Posted 26-Feb-13 11:22am
Sergey Alexandrovich Kryukov 26-Feb-13 17:32pm
If you don't close, what are you hoping to gain? :-)

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You should, like in this code sample:[^].

If you don't close, your modified Registry data can be lost, you need to flush it to the stream, no matter file or network:[^].

Why would you think that you can skip it? And why?

namerg 26-Feb-13 17:38pm
Thanks Sergey. Well, in vbscript you do not. So, what's the difference between subkey1.Dispose(); and subkey.Close(). I did use Dispose but the app crashed. Thanks for your help.
Sergey Alexandrovich Kryukov 26-Feb-13 17:42pm
Sorry, I did not try. There is not "crashed"; this must be an exception. Could you show comprehensive exception information?
I would suggest you use "using statement" (not using clause, using statement). You can do it because IDisposable is supported:

using (RegistryKey key = RegistryKey.OpenRemoteBaseKey( /* ... */ )) {
// use key here...
} // automatically disposed here

namerg 26-Feb-13 17:59pm
by the way, I changed it to close and I got the following error: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of object. as .Program.Main(String[] args
And the event viewer shows the following:
Application: UPI_IE9_GE_Settings.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
at UPI_IE9_GE_Settings.Program.Main(System.String[])
Faulting application name: UPI_IE9_GE_Settings.exe, version:, time stamp: 0x512d396d
Faulting module name: unknown, version:, time stamp: 0x00000000
Exception code: 0xc0000005
Fault offset: 0x006925b4
Faulting process id: 0x128
Faulting application start time: 0x01ce1472486044f1
Faulting application path: C:\script\UPI_IE9_GE_Settings.exe
Faulting module path: unknown
Report Id: 86485231-8065-11e2-88d9-1803733d940c
Sergey Alexandrovich Kryukov 26-Feb-13 18:02pm
This exception is too easy to detect and fix... Can you execute it under the debugger?
namerg 26-Feb-13 18:32pm
working on it...
namerg 26-Feb-13 18:38pm
I got it, but not sure how to fix it. At the end I have Subkey17.close() Subkey17a.close() Subkey17b.close() and of course Subkey17a is null so, there is nothing to close. I can do "if subkey17a == null then break else close it, right ?
Sergey Alexandrovich Kryukov 26-Feb-13 18:49pm
Well, I told you it's easy :-). Now check for null before dereferencing every time, and, in case of null, do nothing. Isn't it simple?
namerg 26-Feb-13 18:56pm worked on my console app but on the windows version it failed...gotta debug the windows version. But, you are right thank you so much. If i get stuck, i will bother you :)
Sergey Alexandrovich Kryukov 26-Feb-13 19:00pm
You are welcome. I think you need to pay some more effort, just a bit more, to get it.
Good luck,
Sergey Alexandrovich Kryukov 26-Feb-13 19:01pm
By the way, will you accept the answer formally (green button)?
I hope the principle issues are already resolved, you only have to put it to work.
namerg 26-Feb-13 19:01pm
Got it. I just finished my first C# program. Thank you so much for you gayness intended :) You da man.
Sergey Alexandrovich Kryukov 26-Feb-13 19:11pm
You are very welcome.
Thank you for your nice words.
Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170422.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100