Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-07
4
Medium Priority
?
1,373 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:
Meir Rivkin 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:Meir Rivkin
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

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 Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
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 use and create keystrokes in Netbeans IDE 8.0 for Windows.

604 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