Category Archives: C#

“Object reference not set to an instance of an object.” when using OpenSubKey

Whilst using a working project on my new development machine, I came across an issue that stumped me for a while. Microsoft.Win32.Registry.LocalMachine.OpenSubKey(“subkey”).GetValue(“blah”) was failing with the error above, even though it worked perfectly previously and the key was clearly in the registry. After a bit of research I found it to be because I’m now [...]

Run only one instance of a program in C#

This example shows you how to ensure that only one instance of an application is running. // Ensure only one instance of this application is running. bool alone = false; Mutex m = new Mutex(true, "<name>", out alone);   if (!alone) { Console.WriteLine("Another instance is already running."); Console.ReadKey(); return; }   Console.WriteLine("Running…\n"); Console.ReadKey();   // [...]