Solved

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

Posted on 2010-11-07
4
1,230 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Class object 2 28
Run time Error 4 35
C# parent child form 5 20
How to check the existence of a URL using vb.net which should detects its existence even if it is blocked 8 21
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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now