Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
<pre>public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription)
        {
            RegistryKey BaseKey;
            RegistryKey OpenMethod;
            RegistryKey Shell;
            RegistryKey CurrentUser;

            BaseKey = Registry.ClassesRoot.CreateSubKey(Extension);
            BaseKey.SetValue("", KeyName);

            OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName);
            OpenMethod.SetValue("", FileDescription);
            OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\",0");
            Shell = OpenMethod.CreateSubKey("Shell");
            Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\"");
            Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\"");
            BaseKey.Close();
            OpenMethod.Close();
            Shell.Close();


            // Delete the key instead of trying to change it
            CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.ucs", true);
            CurrentUser.DeleteSubKey("UserChoice", false);// ERROR HERE  System.NullReferenceException object reference not set to an instant of an object
            CurrentUser.Close();

            // Tell explorer the file association has been changed
            SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
        }

        [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);


What I have tried:

i'm debugging the program in Visual Studio after right clicking on visual studio and choosing run as admin.
Posted
Updated 10-Nov-16 0:00am
v3
Comments
Richard MacCutchan 10-Nov-16 6:51am    
It would also help you if you put some proper error checking into your code.
john1990_1 10-Nov-16 6:56am    
see below please
Richard MacCutchan 10-Nov-16 7:01am    
What?
john1990_1 10-Nov-16 7:02am    
please see what i commented on the answer
Richard MacCutchan 10-Nov-16 7:08am    
I don't see what that has to do with my suggestion. Whether you are a hobbyist or a professional you should use all the features that make life easy for yourself. And catching NULL references is the most basic one.

1 solution

You get it because "CurrentUser" is null, if you learn to debug your code, or googled the error message you could have discovered that yourself. It will be null because the OpenSubKey line above returned null.

If you consult the documentation for OpenSubKey

RegistryKey.OpenSubKey Method (String, Boolean) (Microsoft.Win32)[^]

you'll see that it returns null when the key doesn't exist. So amend your code to create the node if it doesn't exist.
 
Share this answer
 
Comments
john1990_1 10-Nov-16 6:56am    
i'm a hobbyist but a bit advanced and know what null exception is, but what should i do so it doesn't return null?
F-ES Sitecore 10-Nov-16 7:10am    
after doing the open key you check if CurrentUser is null and if it is you create the key. That way the first time the code runs it will create the key, and from them on it will use the key previously created. Or if it runs on a machine with the key already there it is used.
john1990_1 10-Nov-16 7:44am    
i partially understood, would u explain more or edit the code please?
F-ES Sitecore 10-Nov-16 9:14am    
code would be like

CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.ucs", true);
if (CurrentUser == null)
{
// create the key here
}

However if you look at the documentation for CreateSubKey

https://msdn.microsoft.com/en-us/library/ad51f2dx(v=vs.110).aspx

it actually does what you want, it opens it if it exists or creates it if it doesn't, so rather than OpenSubKey just use CreateSubKey
john1990_1 10-Nov-16 7:04am    
maybe i should replace "UserChoice" with something?

I call:
SetAssociation(".sudoku", "SUDOKU_File", Application.ExecutablePath, "SUDOKU File");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900