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:
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.