简体   繁体   中英

C# What is the best way to write the contents of a comboBox to the registry?

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

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?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM