Registry módosítása

155 0

Új elem hozzáadása és kiolvasása registry-ből

using Microsoft.Win32;
using System;
using System.Windows.Forms;

namespace RegistryValues
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Define the registry values as a string
            string registryValues = @"
                Windows Registry Editor Version 5.00
                [HKEY_CURRENT_USER\ExampleKey]
                "ExampleValue"="Example Value"
                "ExampleNumber"=dword:0000000a
            ";

            // Split the string into lines
            string[] lines = registryValues.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            // Iterate through the lines and write the values to the registry
            foreach (string line in lines)
            {
                if (line.StartsWith("[") && line.EndsWith("]"))
                {
                    // This is a registry key path
                    string keyPath = line.Trim('[', ']');
                    Registry.SetValue(keyPath, null, null, RegistryValueKind.None);
                }
                else if (!string.IsNullOrWhiteSpace(line) && line.Contains("="))
                {
                    // This is a registry value
                    int index = line.IndexOf('=');
                    string name = line.Substring(0, index).Trim();
                    string value = line.Substring(index + 1).Trim();

                    // Determine the value kind based on the value string
                    RegistryValueKind valueKind;
                    if (value.StartsWith("\"") && value.EndsWith("\""))
                    {
                        // String value
                        value = value.Trim('\"');
                        valueKind = RegistryValueKind.String;
                    }
                    else if (value.StartsWith("dword:"))
                    {
                        // DWORD value
                        value = value.Substring("dword:".Length);
                        valueKind = RegistryValueKind.DWord;
                    }
                    else
                    {
                        // Unknown value kind
                        valueKind = RegistryValueKind.Unknown;
                    }

                    // Write the value to the registry
                    Registry.SetValue(@"HKEY_CURRENT_USER\ExampleKey", name, value, valueKind);
                }
            }
        }
    }
}