Not Closing the Registry Key

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I am only reading the registry (not updating) is it necessary to close the registry key ? The code in question is in a data access component that gets hit pretty hard from the .aspx pages. Here is my code

string sUserId=""
string sPassword=""
RegistryKey rk = Registry.LocalMachine.OpenSubKey(KeyName)
if ( rk == null ) {return null;
sConnectionString = (string)rk.GetValue("ConnectionString")
sUserId = (string)rk.GetValue("UserId")
sPassword = (string)rk.GetValue("Password")
if (sUserId!=null) {sConnectionString = sConnectionString + "Uid=" + sUserId + ";";
if (sPassword!=null) {sConnectionString = sConnectionString +"Pwd=" + sPassword + ";";
return sConnectionString
 
Yes.
You must explicitly close any object that works with a system.
Like RegestryKey, File, Socket,....

George.

jzink said:
If I am only reading the registry (not updating) is it necessary to close
the registry key ? The code in question is in a data access component that
gets hit pretty hard from the .aspx pages. Here is my code:
 
What type of error would I see if I am not closing it
We had our production IIS site crash yesterday and needed MS to help us get it back up. Turned out that the account "Network Service" lost it's permissions to read the registry. Couldn't get the site to come backup. MS suggested adding read access at the hklm level for "Network Service". Made the change and it worked. Now I am trying to figure out what caused the crash

----- George Ter-Saakov wrote: ----

Yes
You must explicitly close any object that works with a system
Like RegestryKey, File, Socket,...

George

jzink said:
If I am only reading the registry (not updating) is it necessary to clos
the registry key ? The code in question is in a data access component tha
gets hit pretty hard from the .aspx pages. Here is my code
 
You will not get any error.
You will drain resources eventually and the server will stop functioning.
It might even crash.

George.

jzink said:
What type of error would I see if I am not closing it ?
We had our production IIS site crash yesterday and needed MS to help us
get it back up. Turned out that the account "Network Service" lost it's
permissions to read the registry. Couldn't get the site to come backup. MS
suggested adding read access at the hklm level for "Network Service". Made
the change and it worked. Now I am trying to figure out what caused the
crash.
 
My understanding is that Open registry key is a windows Handle.
You can see how many windows handles process has open by using task manager.
View/SelectColumns/HandleCount.

But this number includes every handle (open files )

George.
 
Theoretically no.
But nobody knows what can happens when program stops working correctly.
If it crashed when server was updating the registry then it could got
corrupted.

George.
 
Back
Top