I’m writing some C# .net code that saves some values to the registry. It worked fine up until I wanted to save some binary data.
I have a List<MyType> object where MyType looks like this:
[Serializable] public class MyType
{
public string s {get;set;}
public string t {get;set;}
}
I get an error with the following code:
List<MyType> objectToSaveInRegistry = getList();
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(MySpecialKey, true);
registryKey.SetValue("MySpecialValueName", objectToSaveInRegistry , RegistryValueKind.Binary);
The error is: “The type of the value object did not match the specified Registry ValueKind or the object could not be properly converted.”
What can I do so that I can save my object in the registry?
You probably need to serialize your object first with the help of the BinaryFormatter and store it in a
bytearray which you then can pass toSetValue. I doubt thatSetValuewill serialize the object for you.Quick example: