Solved

C# Open registry " * " system read error

Posted on 2010-11-08
5
483 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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!
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

691 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