Solved

C# Open registry " * " system read error

Posted on 2010-11-08
5
481 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
[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
  • 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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