Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

746 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

10 Experts available now in Live!

Get 1:1 Help Now