Tutorial :C#: Storing a .Net Object in the Registry



Question:

Is it possible to store a .Net object in the registry?

I want to store a generic List<> in the registry, and then retrieve it and parse it back to the List.

Is this possible, or do I need to manually serialize it and then deserialize it back?

[UPDATE]

Following the posted answers, I am going to serialize the object and save it in the Current User's AppData folder.


Solution:1

Why the registry?

The appropriate place to store these kind of serialised objects is usually in the users' Application Data folder or Isolated Storage. Though of course the method of serialisation is up to you. XML, binary etc, it's essentially a file on disk.

You could consider things like "Local Database" or SQL Server Express, depending on your data and concurrency needs.

Some applications do store a Most Recently Used (MRU) list in the registry, but that's just by iterating all the values of a given key. I don't recommend that approach.


Solution:2

It's possible, if the type included in the list is serializable. If that's the case, you can just serialize it and store it in a string value.

But it's probably a very bad idea. The registry gets big enough as it is. Instead, put this kind of thing on the file system, in the All Users Application Data folder.


Solution:3

Yeah, I think you'd have to serialize and deserialize it yourself. But you could store it either as a binary block or text/xml. It's possible that there is a size limit to registry data...

The big question is "is this a good thing to do?"


Solution:4

You will have to serialize it yourself. Beware that there might be limitations on the amount of data you can store, depending on the Windows version.

http://msdn.microsoft.com/en-us/library/ms724872(VS.85).aspx


Solution:5

I'll just say this first: this sounds like a really bad idea.

If you insist on doing this, you're going to have to serialize it first. The registry doesn't support inserting .NET objects.


Solution:6

You would have to serialize it. The registry only stores primitive values.


Solution:7

You would need to manually serialize it.


Solution:8

Not quite sure why you'd want to store .NET objects in the registry, as there's already existing functionality in the BCL that allows you to do this with XML configuration files... but saying that, it is of course possible to store .NET objects in the registry. You'd probably only want to do it if the size of the object was relatively small, but it shouldn't be a problem anyway. I guess that the obvious way to do it would be to use XML Serialization (without formatting/whitespace) and store the object as a serialized string value.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »