?
Solved

C# registry error: "cannot read closed registry key"

Posted on 2010-11-07
4
Medium Priority
?
1,341 Views
Last Modified: 2013-12-16
Hi there! I have a C# code that reads registry key values but the console now states that I can't read the registry key even though I have set full permissions for the key! My regedit is able to read the key and its values.

Can someone please run the codes and tell me whats wrong or is it just the UAC problem for Vista? Run as console command type for Visual Studio 2010.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace Typed_URLS
{
    class Program
    {
        static void Main(string[] args)
        {
            RegistryKey rk = Registry.CurrentUser;

            rk = rk.OpenSubKey("Software\\Microsoft\\Internet Explorer\\TypedURLs", false);

            rk.Close();

            if (rk == null)
            {
                Console.WriteLine("Couldn't open the desired subkey.");
                return;
            }

            try
            {
                string[] valnames = rk.GetValueNames();
                int i = 0;

                foreach (string s in valnames)
                {
                    string val = (string)rk.GetValue(valnames[i++]);
                    Console.WriteLine(s + " contains " + val);
                }
            }

            catch (Exception MyError)
            {
                Console.WriteLine("An errors has occurred: " + MyError.Message);
            }

            Console.WriteLine("-----------------------------------------------");
        }
    }
}

Open in new window

0
Comment
Question by:VMthinker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 total points
ID: 34082302
on line 17 you close the registry key, thats why u get this error.move this line to 'finally' scopy in the try/catch:using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Win32;namespace Typed_URLS{    class Program    {        static void Main(string[] args)        {            RegistryKey rk = Registry.CurrentUser;            rk = rk.OpenSubKey("Software\\Microsoft\\Internet Explorer\\TypedURLs", false);                      if (rk == null)            {                Console.WriteLine("Couldn't open the desired subkey.");                return;            }            try            {                string[] valnames = rk.GetValueNames();                int i = 0;                foreach (string s in valnames)                {                    string val = (string)rk.GetValue(valnames[i++]);                    Console.WriteLine(s + " contains " + val);                }            }            catch (Exception MyError)            {                Console.WriteLine("An errors has occurred: " + MyError.Message);            }finally{  rk.Close();}            Console.WriteLine("-----------------------------------------------");        }    }}
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 34082311
the finally scope will be executed no matter what, which guarantee the registry key will be closed.
0
 
LVL 2

Author Closing Comment

by:VMthinker
ID: 34090221
Awesome answer and explination! Thanks dude!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question