I had a problem trying to create an XML file from the registry. It works fine on my laptop (W7 64b), an xml file is generated, but an exception is thrown on another computer (Xp 32b): System.ArgumentException '.', Hexadecimal values ββ0x00, is an invalid character. I read some useful things about this, but I donβt know how to solve in this case, here is the code:
try { string regPath = "SOFTWARE\\IPS"; XElement xRegRoot = new XElement("Root", new XAttribute("Registry", regPath)); ReadRegistry(regPath, xRegRoot); string xmlStringReg = xRegRoot.ToString(); XmlDocument docR = new XmlDocument(); docR.LoadXml(xmlStringReg); docR.Save(AppDomain.CurrentDomain.BaseDirectory + "\\_RegistryList.xml"); } catch (System.Exception ex) { Console.WriteLine(ex.ToString()); LogToFile(ex.ToString()); } private static void ReadRegistry(string keyPath, XElement xRegRoot) { string[] subKeys=null; RegistryKey HKLM = Registry.LocalMachine; RegistryKey RegKey = HKLM.OpenSubKey(keyPath); try { subKeys = RegKey.GetSubKeyNames(); foreach (string subKey in subKeys) { string fullPath = keyPath + "\\" + subKey; Console.WriteLine("\r\nKey Name | " + fullPath); LogToFile("Key Name | " + fullPath); XElement xregkey = new XElement("RegKeyName", new XAttribute("FullName", fullPath), new XAttribute("Name", subKey)); xRegRoot.Add(xregkey); ReadRegistry(fullPath, xRegRoot); } string[] subVals = RegKey.GetValueNames(); foreach (string val in subVals) { string keyName = val; string keyType = RegKey.GetValueKind(val).ToString(); string keyValue = RegKey.GetValue(val).ToString(); Console.WriteLine("Key Value | " + keyType + " | " + keyName + " | " + keyValue); LogToFile("Key " + keyType + " | " + keyName + " | " + keyValue); XElement xregvalue = new XElement("RegKeyValue", new XAttribute("keyType", keyType), new XAttribute("keyName", keyName), new XAttribute("keyValue", keyValue)); xRegRoot.Add(xregvalue); } } catch (System.Exception ex) { Console.WriteLine(ex.ToString()); LogToFile(ex.ToString()); } }
Thanks in advance.
c # xml registry
Owens
source share