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

x
?
Solved

C# Open registry " * " system read error

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

618 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