Beispiel #1
0
        /// <summary>
        /// Writes all files described in "RegMappings.xml" to the registry in HKEY_CURRENT_USER.
        /// </summary>
        /// <param name="keyName">Registry key to write the values to.</param>
        /// <param name="dir">Directory in which the value files and "RegMappings.xml" file is stored.</param>
        public static void WriteHKCUBinaryValuesFromFiles(string keyName, string dir)
        {
            string fileDirPath = Path.GetFullPath(dir);

            var      xDocument = XDocument.Load($"{fileDirPath}/RegMappings.xml");
            XElement mappings  = xDocument.Element("mappings");

            using (RegistryKey keyToWrite = Registry.CurrentUser.OpenSubKey(keyName, RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                foreach (XElement xElement in mappings.Elements("mapping"))
                {
                    Console.WriteLine($"Writing contents of file '{xElement.Value}' to key '{keyName}' and value '{xElement.Attribute("regValueName").Value}'.");
                    string fileContent = File.ReadAllText($"{fileDirPath}/{xElement.Value}");
                    byte[] bytes       = new UTF8Encoding().GetBytes(fileContent);
                    //Add null byte for registry
                    bytes = bytes.Append((byte)0x00).ToArray();

                    keyToWrite.SetValue(xElement.Attribute("regValueName").Value, bytes, RegistryValueKind.Binary);
                }
            }
        }