Hi @Gennady Gurin , Welcome to Microsoft Q&A,
You're trying to call Key.Close() regardless of whether Key is null.
If the registry key doesn't exist (Key == null), attempting to call Key.Close() will result in a NullReferenceException.
private void Form1_Load(object sender, EventArgs e)
{
// Attempt to open the registry key
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\MyApp"))
{
// Check if the key exists
if (key == null)
{
Activated_Bool = false; // Key does not exist
}
else
{
Activated_Bool = true; // Key exists
}
} // The 'using' statement ensures the key is properly closed
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.