Solved

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

Posted on 2010-11-07
4
1,246 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
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

776 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