tags:

views:

315

answers:

4

I have a comboBox that is filled at run time when a button is clicked (approx 6 items read in from a database), I want to write these comboBox items to the registry so I can read them back when the program loads so the comboBox is already populated without need to reread them from the database.

My question is what is the best way to write these to the registry and how do I write the selectedItem to the registry so that it is the selectedItem when the program loads?

Many thanks

+6  A: 

You don't say why you want to avoid the database, but I guess it's latency, maybe a pause while the UI retrieves data?

In that case perhaps you should consider a local cache of data in an Application Data folder rather than writing to the registry?

Ed Guiness
I used to have a textBox with one value that I wrote to the registry and now I have 6 items I'd like to continue pretty much the same. Reading from the DB when the program loads would be too slow. Thanks
@edg. I agree. Isn't the registry essentially deprecated for application development?
IainMH
My Documents is not a good spot to store an application cache. It is pretty likely a user will delete it, and if they find it they will certainly wonder why it is mixed with all their documents.Application Data is a much better choice.
ScottS
@ScottS, agreed and updated
Ed Guiness
+4  A: 

In my opinion the best way is to not pollute registry and save this information to the plain text file. Also when your program isloaded data in database can be changed so you should check if such optimization worth it. Anyway if you still want to write this data to registry you can use Registry class.

aku
A: 

One way to do this would be to serialize the ComboBox's Items property - XML serialization would probably be easiest in this case. You could then save the resulting text to a file, Isolated Storage, or to the registry using the RegistryKey class - as long as the serialized data isn't too large, for 6 items it would be fine. Then simply deserialize the data back to the combo box on app startup.

Stu Mackellar
A: 

Save to a config file with a dictionary on the form close, and on load read them out and back into the combo box. http://msdn.microsoft.com/en-us/library/cc221360(VS.95).aspx

xoxo