?
Solved

C# Open registry " * " system read error

Posted on 2010-11-08
5
Medium Priority
?
493 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 9

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 9

Expert Comment

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

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 9

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 2000 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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!
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

809 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