User Preferences
I'm still very new to writing windows apps so go easy on me.
I'm writing a program that is basically a food journal for people doing low-carb diets. There are several things that have been asked for that should really be options and I'm wondering what is the best way of storing and retrieveing these options? An example would be wether weight is shown in imperial or metric for instance.
Are .ini files still the way to go? If so I take it I just treat this like a text file and read in the preferences on startup?
Thanks
Morkai
Rather than using INI files, it would be easier to use an XML file--the .NET Framework provides rich support for reading and writing XML, but just about none for working with INIfiles (although you could add support yourself, it hardly seems worth it). You can also store information in the registry, if you must <g>.
The question is where to put this information--most people use code something like this to get the folder within the Documents and Settings folder for the current user:
Environment.GetFolderPath( _
Environment.SpecialFolder.ApplicationData)
Check out the support for reading and writing XML in the .NET Framework--much has been written on this topic, and you'll find a comprehensive example within the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=6703E468-3F4B-483B-993A-8DC4D2F3AC3A&displaylang=en">101 VB.NET samples</a>.
i couldn't agree more when Ken said about XML, and if you must have support for unstrucured confuguration may be XML file is the best, but Ken forgot to mention about the .config file, for a simple key-value combination .config file is the best and you can write your own config handlercheck out IConfigurationSectionHandler interface for more details
I definitely agree with both posts that XML is the way to go, however, I disagree that the settings should be put in the config file. True it's a nice name value collection, but that file is more for application specific settings. If you are going to store <b>user</b> specific settings, then I would store then in an XML file in IsolatedStorage. There are some nice classes to help you do that in System.IO.IsolatedStorage. There's also a lot of help on it in the MSDN Library.
Well, having spent a lot of time on this recently, I'll reply to both posts.
The app.exe.config file is great for default user settings, but it's the .NET Framework doesn't include support for writing to this file <b>on purpose</b>. Because the config file is generally in a location where the user may not have rights to write, and because it's likely to be in a shared location, you should never, under any circumstances, get the idea to <b>write</b> to the app.exe.config file. Some day, you'll regret it if you do.
I just spent some time researching and writing a magazine article on isolated storage, and yes, it is a good choice for user settings. Funny how it slipped my mind, since I had just written 4000 words on it! <g> If you're interested in the article, I can send a preview copy to you via email, although it's also available to subscribers <a href="http://accessvbsqladvisor.com/doc/11990">here</a>. You can download the sample application there, as well.
Apologies for the delay in replying. I just got a new job so life is a bit hectic. Anyway I tried to find the issue of that magazine over the weekend but was unable too - if you could be so kind as to send me that preview I'd be extremely greatful.
Thanks for everyones insights, I kept wondering when I would find a use for xml :)
Regards
Morkai
my suggestion for using .config for storage may not be the best for some, but i use it regularly, especially for storing somethings that doesn't change much or some setting that you set up during the deployement/installation process. there's even an article in MSDN about deploying and writing(they use XMLDocument) into .config file so it's not as bad as some might think, and i always keep a spare .config file when something bad happen.
but as said, i always prefer the database or XML file for user preferences, that might change a lot such as skin, themes etc... and again in this case he is talking about soemthing like unit, it's unlikely you want to change this every now and then, i live in a region when we talk kilometers not mile, so why should i change my unit preferences every forthnight. i would normally specify this during the deployment. and live with it. it's just like your database connection string.
I shall look into the various options. I'm not sure yet exactly what options I will be giving users, it depends a lot on how things develop. Still here are certain things that yes will be set once when the user first loads and looks around. I may even set up a small wizard to do that as there is going to be a need for a certain amount of user information to be stored if they want graphs and reports.
At least I have a idea on where to start looking now to find out more, I've never worked with xml yet so it should be an interesting journey :)
Thanks again
Morkai
the thing you have to think about in your case though is: Are you going to have more than one user on a single machine set this application up. If you store the user data in the config file, that will work fine, if only one person on that machine is going to use it. If you have two or more, then each of them would overrite the other's settings. Just something to think about.
mmm, thats a good point - I will be setting up the ability for multiple users so okay - seperate xml files it is.
Thanks kindly
Morkai
Morkai,
I asked a similar question about a month ago and used Ken's article to write a app demonstrating Isolated Storage.
If you would like a copy e-mail me and let me know?
I would, but you don't seem to have a public email addy viewable :)
Morkai