tags:

views:

816

answers:

8

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.

+1  A: 

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

Mark S. Rasmussen
A: 

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

John Saunders
A: 

You would need to manually serialize it.

Jason Coyne
+5  A: 

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.

Neil Barnwell
+2  A: 

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?"

Jeff Kotula
+1  A: 

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.

Alex Fort
+5  A: 

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.

Joel Coehoorn
A: 

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.

Noldorin