Backup Registry Key Values

Peter Volz 1,295 Reputation points
2023-04-10T03:04:52.2666667+00:00

Hello all I need to backup and restore a registry key using vb.net (or c#), key is Current User, no need special access grant: HKEY_CURRENT_USER\SOFTWARE\ACME\Product\KeyToBackup Just need the values under the key to be saved, I mean if there are sub keys inside, no need them. Is there any .net method for that (.net fw 4 full profile) or I just read them manually and save to a file? And about the save format, what you guys recommend?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,364 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,600 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 35,556 Reputation points Microsoft Vendor
    2023-04-10T14:06:10.3733333+00:00

    Hi @Peter Volz ,Welcome to Microsoft Q&A. Do you mean you want to get the default value of the registry "HKEY_CURRENT_USER\SOFTWARE\ACME\Product\KeyToBackup" and backup and restore it? Manipulating the current user's registry does not require permissions. I created a project using Winform (.Net Framework 4.8). There are two methods: one is to query whether the target key exists, and if it exists, it will save the default value of the target key in the target txt file. The second method is to find out whether the target backup file exists, and restore the key value if it exists. Here's the code:

    using Microsoft.Win32;
    using System;
    using System.IO;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            string keyPath = @"SOFTWARE\ACME\Product\KeyToBackup"; // registry key path
            string backupFilePath = @"C:\backup.txt"; // Registry backup file path
    
    
            static void BackupRegistryKey(string keyPath, string backupFilePath)
            {
                // Check if the registry key exists
                RegistryKey regKey = Registry.CurrentUser.OpenSubKey(keyPath);
                if (regKey == null)
                {
                    Console.WriteLine("Registry key does not exist: " + keyPath);
                    return;
                }
    
                // Backup registry keys to text file
                using (StreamWriter writer = new StreamWriter(backupFilePath))
                {
                    string[] valueNames = regKey.GetValueNames();
                    foreach (string valueName in valueNames)
                    {
                        if (valueName == @"")
                        {
                            string value = regKey.GetValue(valueName)?.ToString();
                            writer.WriteLine($"{valueName}={value}");
                        }
                    }
                }
            }
    
            static void RestoreRegistryKey(string keyPath, string backupFilePath)
            {
                // Check if the backup file exists
                if (!File.Exists(backupFilePath))
                {
                    Console.WriteLine("Backup file does not exist: " + backupFilePath);
                    return;
                }
    
                // Read registry backup and restore from text file
                using (StreamReader reader = new StreamReader(backupFilePath))
                {
                    string line;
                    // Pay attention to use the open method with write permission
                    RegistryKey regKey = Registry.CurrentUser.OpenSubKey(keyPath, true);
                    if (regKey == null)
                    {
                        Console.WriteLine("Registry key does not exist: " + keyPath);
                        return;
                    }
    
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] parts = line.Split('=');
                        string valueName = parts[0];
                        string valueData = parts[1];
    
                        // restore registry keys
                        regKey.SetValue(valueName, valueData);
                        Console.WriteLine($"Restored value: {valueName}={valueData}");
                    }
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                // Backup registry keys to text file
                BackupRegistryKey(keyPath, backupFilePath);
                Console.WriteLine("Registry key backed up to: " + backupFilePath);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                // Read registry backup and restore from text file
                RestoreRegistryKey(keyPath, backupFilePath);
                Console.WriteLine("Registry key restored successfully.");
            }
        }
    }
    
    

    Here's the demo:

    enter image description here

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful