Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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.
//*************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 26-Feb-13 11:22am
namerg384
Comments
Sergey Alexandrovich Kryukov at 26-Feb-13 17:32pm
   
If you don't close, what are you hoping to gain? :-)
—SA

1 solution

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

Solution 1

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
  Permalink  
Comments
namerg at 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 at 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 at 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 at 26-Feb-13 18:02pm
   
This exception is too easy to detect and fix... Can you execute it under the debugger?
—SA
namerg at 26-Feb-13 18:32pm
   
working on it...
namerg at 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 at 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?
—SA
namerg at 26-Feb-13 18:56pm
   
Damn...it 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 at 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,
—SA
Sergey Alexandrovich Kryukov at 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.
Thanks,
—SA
namerg at 26-Feb-13 19:01pm
   
Got it. I just finished my first C# program. Thank you so much for you help..love you...no gayness intended :) You da man.
Sergey Alexandrovich Kryukov at 26-Feb-13 19:11pm
   
You are very welcome.
Thank you for your nice words.
Good luck, call again.
—SA

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

  Print Answers RSS
0 OriginalGriff 398
1 Richard Deeming 210
2 Sergey Alexandrovich Kryukov 202
3 PhilLenoir 120
4 RyanDev 115
0 Sergey Alexandrovich Kryukov 6,405
1 OriginalGriff 5,798
2 CPallini 2,473
3 Richard MacCutchan 1,647
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
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