Solved

C# Open registry " * " system read error

Posted on 2010-11-08
5
470 Views
Last Modified: 2013-12-17
Hi There! I have a simple program which is to read the recent save files from Windows XP registry. However there is an error upon running the program through the Visual Studio 2010 console mode for c#. The error out put is "Object reference not set to an instance of an Object"

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

namespace Tracking_Files
{
    class ConsoleApplication1
    {
        static void Main(string[] args)
        {
            try
            {
                RegistryKey rk = Registry.CurrentUser;

                rk = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU", false);
                PrintKeys(rk);
            }

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

        static void PrintKeys(RegistryKey rk)
        {
            if (rk == null)
            {
                Console.WriteLine("No specified registry key!");
                return;
            }

            String[] names = rk.GetSubKeyNames();

            Console.WriteLine("Subkeys of " + rk.Name);
            Console.WriteLine("-----------------------------------------------");

            foreach (String s in names)
            {
                Console.WriteLine(s);

                RegistryKey rk2 = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU" + s, false);

                String[] reg = rk2.GetSubKeyNames();

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

                    Console.WriteLine("Subkeys of " + rk2.Name);
                    Console.WriteLine("-----------------------------------------------");

                    string[] valnames = rk2.GetValueNames();

                    int i = 0;

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

                    rk.Close();
                }

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

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

Open in new window


I have narrowed down the error to the line :

RegistryKey rk2 = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU" + s, false);

Open in new window


When I add in either Etc. " * "or "docs" instead of "+ s", it works perfectly! So is there anyway that I can let the program/system to understand the "*" is a directory while opening the key?

Thanks!
0
Comment
Question by:VMthinker
  • 4
5 Comments
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 34090564
In line 44 Rk2 is null, after that forcibly ur tring to get the subkeys So getting exception. Solution might be before proceeding to getting the subkeys, lets check up whether rk2 is null or null!
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 34090565
Typo------>
null or not.
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 34090591
RegistryKey rk2 = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU\\" + s, false);
0
 
LVL 8

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 500 total points
ID: 34090636
If want to use single slash '\' then keep the string after @
Ex:
@"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU\"


The Complete code is as follows.

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

namespace Tracking_Files
{
    class Program
    {
        const String reg_key = @"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU";

        static void Main(string[] args)
        {

            try
            {
                RegistryKey rk = Registry.CurrentUser;

                rk = rk.OpenSubKey(reg_key, false);
                PrintKeys(rk);
            }

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

        static void PrintKeys(RegistryKey rk)
        {
            if (rk == null)
            {
                Console.WriteLine("No specified registry key!");
                return;
            }

            String[] names = rk.GetSubKeyNames();

            Console.WriteLine("Subkeys of " + rk.Name);
            Console.WriteLine("-----------------------------------------------");

            foreach (String s in names)
            {
                Console.WriteLine(s);

                RegistryKey rk2 = Registry.CurrentUser.OpenSubKey((reg_key + @"\" + s), false);

               
                try
                {
                    if (rk2 == null)
                    {
                        Console.WriteLine("Couldn't open the desired subkey: " +
                                           reg_key + @"\" + s);
                        return;
                    }
                    String[] reg = rk2.GetSubKeyNames();

                    Console.WriteLine("Subkeys of " + rk2.Name);
                    Console.WriteLine("-----------------------------------------------");

                    string[] valnames = rk2.GetValueNames();

                    int i = 0;

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

                    rk.Close();
                }

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

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

        }
    }
}


0
 
LVL 2

Author Closing Comment

by:VMthinker
ID: 34109002
Thanks for the explination! The codes work perfectly!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

13 Experts available now in Live!

Get 1:1 Help Now