Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

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.
C#
//*************BEGIN GENERAL TAB***********************************
            //BROWSING HISTORY/CHECK FOR NEWER VERSIONS OF STORED PAGES

            //Select the RegistryHive you wish to read from 
            RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, "");

            //Select the path within the hive 
            Console.WriteLine("");
            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";
                    break;
                }
                else
                {
                    // 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
Comments
Sergey Alexandrovich Kryukov 26-Feb-13 17:32pm    
If you don't close, what are you hoping to gain? :-)
—SA

1 solution

You should, like in this code sample:
http://msdn.microsoft.com/en-us/library/8zha3xws.aspx[^].

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: http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.close.aspx[^].

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

—SA
 
Share this answer
 
Comments
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

—SA
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
Stack:
at UPI_IE9_GE_Settings.Program.Main(System.String[])
----------------------------
Faulting application name: UPI_IE9_GE_Settings.exe, version: 1.0.0.0, time stamp: 0x512d396d
Faulting module name: unknown, version: 0.0.0.0, 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?
—SA
namerg 26-Feb-13 18:32pm    
working on it...

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900