Get the Registry key found for CurrentUser

image_pdfimage_print
   


using System;
using Microsoft.Win32;


class MainClass {
    public static void SearchSubKeys(RegistryKey root, String searchKey) {
        foreach (string keyname in root.GetSubKeyNames()) {
            try {
                using (RegistryKey key = root.OpenSubKey(keyname)) {
                    if (keyname == searchKey) PrintKeyValues(key);
                    SearchSubKeys(key, searchKey);
                }
            } catch (System.Security.SecurityException) {
            }
        }
    }

    public static void PrintKeyValues(RegistryKey key) {
        Console.WriteLine("Registry key found : {0} contains {1} values",
            key.Name, key.ValueCount);
        foreach (string valuename in key.GetValueNames()) {
            if (key.GetValue(valuename) is String) {
                Console.WriteLine(" Value : {0} = {1}",
                    valuename, key.GetValue(valuename));
            }
        }
    }

    public static void Main(String[] args) {
        if (args.Length > 0) {
            using (RegistryKey root = Registry.CurrentUser) {
                SearchSubKeys(root, args[0]);
            }
        }
    }
}
           
          


Use GetValue and SetValue to get and save value to Registry

image_pdfimage_print
   




using System;
using Microsoft.Win32;
class MainClass {
    public static void Main(String[] args) {
        string lastUser;
        string lastRun;
        int runCount;

        lastUser = (string)Registry.GetValue(@"HKEY_CURRENT_USERSoftwareAC#","", "Nobody");

        if (lastUser == null) {
            lastUser = "Nobody";
            lastRun = "Never";
            runCount = 0;
        } else {
            lastRun = (string)Registry.GetValue(@"HKEY_CURRENT_USERSoftwareAC#","LastRun", "Never");            runCount = (Int32)Registry.GetValue(
                @"HKEY_CURRENT_USERSoftwareAC#","RunCount", 0);
        }
        Console.WriteLine("Last user name: " + lastUser);
        Console.WriteLine("Last run date/time: " + lastRun);
        Console.WriteLine("Previous executions: " + runCount);

        Registry.SetValue(@"HKEY_CURRENT_USERSoftwareAC#","", Environment.UserName, RegistryValueKind.String);
        Registry.SetValue(@"HKEY_CURRENT_USERSoftwareAC#","LastRun", DateTime.Now.ToString(), RegistryValueKind.String);
        Registry.SetValue(@"HKEY_CURRENT_USERSoftwareAC#","RunCount", ++runCount, RegistryValueKind.DWord);
    }
}

           
          


Enumerating Registry Keys

image_pdfimage_print
   



using System;
using Microsoft.Win32;

class Class1 {
    static void Main(string[] args) {
        RegistryKey myRegKey = Registry.LocalMachine;
        myRegKey = myRegKey.OpenSubKey("SOFTWAREMicrosoftWindowsCurrentVersionUninstall");
        String[] subkeyNames = myRegKey.GetSubKeyNames();
        foreach (String s in subkeyNames) {
            RegistryKey UninstallKey = Registry.LocalMachine;
            UninstallKey = UninstallKey.OpenSubKey("SOFTWAREMicrosoftWindowsCurrentVersionUninstall" + s);
            try {
                Object oValue = UninstallKey.GetValue("DisplayName");
                Console.WriteLine(oValue.ToString());
            } catch (NullReferenceException) {
            }
        }
    }
}

           
          


Write a Text and DWord Value to the Registry

image_pdfimage_print
   


using System;
using Microsoft.Win32;

class Class1 {
    static void Main(string[] args) {
        RegistryKey RegKeyWrite = Registry.CurrentUser;
        RegKeyWrite = RegKeyWrite.CreateSubKey("SoftwareCSHARPWriteRegistryValue");
        RegKeyWrite.SetValue("Success", "TRUE");
        RegKeyWrite.SetValue("AttemptNumber", 1);
        RegKeyWrite.Close();

        RegistryKey RegKeyRead = Registry.CurrentUser;
        RegKeyRead = RegKeyRead.OpenSubKey("SoftwareCSHARPWriteRegistryValue");
        Object regSuccessful = RegKeyRead.GetValue("Success");
        Object regAttemptNumber = RegKeyRead.GetValue("AttemptNumber");
        RegKeyRead.Close();

        Console.WriteLine(regSuccessful);
      
    }
}
           
          


Registry.LocalMachine

image_pdfimage_print
   

using System;
using Microsoft.Win32;
using System.Security.Permissions;

[RegistryPermissionAttribute(SecurityAction.Demand)]
class Class1 {
    static void Main(string[] args) {

        RegistryKey myRegKey = Registry.LocalMachine;
        myRegKey = myRegKey.OpenSubKey("SOFTWAREMicrosoftWindows NTCurrentVersion");

        Object oValue = myRegKey.GetValue("RegisteredOwner");
        Console.WriteLine("OS Registered Owner: {0}", oValue.ToString());

    }
}
           
          


new RegexCompilationInfo(@”^d{4}$”,RegexOptions.Compiled, “PinRegex”, “”, true)

image_pdfimage_print
   
 

using System;
using System.Reflection;
using System.Text.RegularExpressions;

    class MainClass
    {
        public static void Main()
        {
            RegexCompilationInfo[] regexInfo = new RegexCompilationInfo[2];
            regexInfo[0] = new RegexCompilationInfo(@"^d{4}$",RegexOptions.Compiled, "PinRegex", "", true);
            regexInfo[1] = new RegexCompilationInfo(@"^d{4}-?d{4}-?d{4}-?d{4}$",RegexOptions.Compiled, "CreditCardRegex", "", true);
            AssemblyName assembly = new AssemblyName(); 
            assembly.Name = "MyRegEx";

            Regex.CompileToAssembly(regexInfo, assembly);
       }
 }

    


Regex.CompileToAssembly

image_pdfimage_print
   
 

using System;
using System.Reflection;
using System.Text.RegularExpressions;

class MainClass {
    public static void Main() {
        RegexCompilationInfo[] regexInfo = new RegexCompilationInfo[2];
        regexInfo[0] = new RegexCompilationInfo(@"^d{4}$", RegexOptions.Compiled, "PinRegex", "", true);
        regexInfo[1] = new RegexCompilationInfo(@"^d{4}-?d{4}-?d{4}-?d{4}$", RegexOptions.Compiled, "CreditCardRegex", "", true);
        AssemblyName assembly = new AssemblyName();
        assembly.Name = "MyRegEx";

        Regex.CompileToAssembly(regexInfo, assembly);
    }
}