Registry Utils : Registry « Development Class « C# / C Sharp

Registry Utils

 * ProfileTool
 * Copyright 2006-2008 Craig Box
 * Released under the Mozilla Public License v1.1

using System;
using System.Collections;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using Microsoft.Win32;

namespace ProfileTool
    class RegistryUtils
        public struct LUID
            public int LowPart;
            public int HighPart;
        public struct TOKEN_PRIVILEGES
            public LUID Luid;
            public int Attributes;
            public int PrivilegeCount;

        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
        public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess,
        ref int tokenhandle);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetCurrentProcess();

        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
        public static extern int LookupPrivilegeValue(string lpsystemname, string lpname,
        [MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
        public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs,
        [MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGES Newstate, int bufferlength,
        int PreivousState, int Returnlength);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int RegLoadKey(uint hKey, string lpSubKey, string lpFile);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int RegUnLoadKey(uint hKey, string lpSubKey);

        public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
        public const int TOKEN_QUERY = 0x00000008;
        public const int SE_PRIVILEGE_ENABLED = 0x00000002;
        public const string SE_RESTORE_NAME = "SeRestorePrivilege";
        public const string SE_BACKUP_NAME = "SeBackupPrivilege";
        public const uint HKEY_USERS = 0x80000003;

        public static void PrepareRegLoad()
            int token = 0;
            int retval = 0;

            LUID RestoreLuid = new LUID();
            LUID BackupLuid = new LUID();

            retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
            retval = LookupPrivilegeValue(null, SE_RESTORE_NAME, ref RestoreLuid);
            retval = LookupPrivilegeValue(null, SE_BACKUP_NAME, ref BackupLuid);
            TP.PrivilegeCount = 1;
            TP.Attributes = SE_PRIVILEGE_ENABLED;
            TP.Luid = RestoreLuid;
            TP2.PrivilegeCount = 1;
            TP2.Attributes = SE_PRIVILEGE_ENABLED;
            TP2.Luid = BackupLuid;

            retval = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
            retval = AdjustTokenPrivileges(token, 0, ref TP2, 1024, 0, 0);

        // The next two functions are thanks to
        /// <summary>
        /// Copy a registry key.  The parentKey must be writeable.
        /// </summary>
        /// <param name="parentKey"></param>
        /// <param name="keyNameToCopy"></param>
        /// <param name="newKeyName"></param>
        /// <returns></returns>
        public static bool CopyKey(RegistryKey parentKey, string keyNameToCopy, string newKeyName)
            //Create new key
            RegistryKey destinationKey = parentKey.CreateSubKey(newKeyName);

            //Open the sourceKey we are copying from
            RegistryKey sourceKey = parentKey.OpenSubKey(keyNameToCopy);

            RecurseCopyKey(sourceKey, destinationKey);

            return true;

        private static void RecurseCopyKey(RegistryKey sourceKey, RegistryKey destinationKey)
            //copy all the values
            foreach (string valueName in sourceKey.GetValueNames())
                object objValue = sourceKey.GetValue(valueName);
                // TODO: .net 1.1 me
        //RegistryValueKind valKind = sourceKey.GetValueKind(valueName);
                destinationKey.SetValue(valueName, objValue); //, valKind);

            //For Each subKey
            //Create a new subKey in destinationKey
            //Call myself
            foreach (string sourceSubKeyName in sourceKey.GetSubKeyNames())
                RegistryKey sourceSubKey = sourceKey.OpenSubKey(sourceSubKeyName);
                RegistryKey destSubKey = destinationKey.CreateSubKey(sourceSubKeyName);
                RecurseCopyKey(sourceSubKey, destSubKey);

    /// <summary>
    /// Convert a RegistryKey to the IntPtr of its handle.
    /// </summary>
    /// <param name="registryKey">RegistryKey object</param>
    /// <returns>Handle of registry key</returns>
    public static IntPtr GetRegistryHandle(RegistryKey registryKey)
      Type type = registryKey.GetType();
      FieldInfo fieldInfo = type.GetField("hkey", BindingFlags.Instance |
      return (IntPtr)fieldInfo.GetValue(registryKey); 

 * RegistryValueKind.cs - Implementation of the
 *      "Microsoft.Win32.RegistryValueKind" class.
 * Copyright (C) 2004  Southern Storm Software, Pty Ltd.
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

namespace Microsoft.Win32
  public enum RegistryValueKind
    Unknown      = -1,
    String      = 1,
    ExpandString  = 2,
    Binary      = 3,
    DWord      = 4,
    MultiString    = 7,
    QWord      = 11

  }; // enum RegistryValueKind

}; // namespace Microsoft.Win32


Related examples in the same category

2.Write a Text and DWord Value to the Registry
3.Enumerating Registry Keys
4.Retrieve the CPU Type and Speed from the Registry
5.Use GetValue and SetValue to get and save value to Registry
6.Get the Registry key found for CurrentUser
7. Accessing the Registry
8.Open a SubKey in Registry
9.Get Registry value Get Registry value
10.Registry File Association
11.Check Registry to see if it is installed
12.Get/Set User Registry
13.Copy Registry
14.Retrieves any Registry value that uses the REGBINARY data type.
15.Writes a Registry value to the Registry.
16.Get IIS version