Click here to Skip to main content
15,936,633 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
In this code it happens that if we close the window with close button, the registry data is cleared. If the EXE crashes, the registry data is not cleared.

Even if the EXE crashes or the application closes or stops debugging, the registry data is cleared. Or when we close the debug from Visual Studio 2019 the registry data should be cleared.

What I have tried:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            // Dispose the VlcControl when the window is closing

            // Clear the registry data
            if (STL.registryy != null)
                // Delete the existing subkey and all its values

                // Create a new subkey
                STL.registryy = Registry.CurrentUser.CreateSubKey("SOFTWARE\\MyApp");
                // Create a new subkey if it doesn't exist
                STL.registryy = Registry.CurrentUser.CreateSubKey("SOFTWARE\\MyApp");
Updated 23-May-23 5:31am
BillWoodruff 23-May-23 8:56am    
Advice: do not use the Registry !

You have an unhandled exception not being caught. You can catch these errors however the method for .Net Framework and .Net Core are different.

I am assuming .Net Framework. Read this: WPF global exception handler - StackOverflow[^]
Share this answer
The problem is once your app crashes, that's it. Your code isn't running anymore, and you don't have a way to clear the registry because of that.

The solution is to write your code in a manner that handles the unexpected exceptions and checks objects before trying to work with them, among other things. It's called "defensive programming."

Oh, and I'd probably move the code that deletes all of your registry data into its own method so you can call it from different places.

Aaaaaand I wouldn't use the registry at all for storing data. If you're going to store data somewhere, even temporarily, use dedicated classes, serialization, and files. Read Where Should I Store My Data?[^]
Share this answer

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