views:

234

answers:

5

Hi,

Just building my first WinForms application.

Question - What's the easiest/best approach for saving some data between use of the application (e.g. list of URL with status & date/time in this case)? I don't see any need for a database.

For example * Is just storing to text file easiest? * Or is storing to XML file just as easy in DotNet * How about Windows Registry - is this something generally to avoid? Is it's use compatible across all versions including Windows 7 * Database - probably overkill here * A widely used library perhaps?

Thanks

+4  A: 

If you really need heavy persistence and out of application storage you could use sqlite or sql server compact (both standalone) however in your case I think reading/writing to an xml file in the common application data folder would suit your needs just fine and is increadibly easy.

Quintin Robinson
A: 

Edited because my first post was clear as mud

Personally, I like storing data in a database if users share data.

For non-shared, single user data, the following options are all pretty easy.

If your data is stored in a DataSet internally, I would suggest using DataSet.WriteXml and DataSet.ReadXml for storing locally.

Otherwise a plain text file would be teh easiest for something as simple as you suggested. If the data is going to be more complex, however, then writing an Xml document would be most appropriate.

You could also look into Serialization:

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

David Stratton
+3  A: 

Storing to XML is very easy in .NET, particularly if you're really just storing a URL and a timestamp. You could create a custom class to hold the data... at runtime, manipulate instances of that class in your app.

When it's time to save, serialize the object(s) to XML... when the app needs to restore the data later, just deserialize. MSDN has a simple walkthrough.

It's worth noting, as Quintin did, that using SQL Server Compact or some other lightweight database might also be a good idea. XML's quick and easy - but if you need people to share data or you need anything more flexible than simple serialization, you're better off with a database.

Jeff Donnici
BY THE WAY - SQL Server Compact would require a separate installation by the user? Or could you effectively seemlessly package it's deployment as part of the application click-once deployment?
Greg
+7  A: 

Take a look at the Settings of your project (right-click project in Solution Explorer > Properties > Settings tab), you can define a number of variables that are persisted throughout uses of the program for each user, like username, last update time, proxy server, anything like that. The settings themselves are serialized to XML and live in the Application Settings folder for each user, but you can specify default or application-specific settings as well.

You can then use the settings like this:

MyNamespace.Properties.Settings.Default.MySetting

More info about settings files can be found @ MSDN or the Code Project

This is great if you only need to store a few variables between sessions. If you need to store a larger amount of data, look into either one of the database options suggested in the other answers, or serialization.

Dale Halliwell
A: 

You should check out db4o. This lightweight open-source object database for .NET and Java objects is very useful when you simply want simple persistence. Its more query-able than a text file and not as heavy as an RDBMS. Its included as a library and persists to a file, so there are no out-of-process calls.

It can be added to your project by referencing the db4o library (download here). You give it a file path which you want to persist objects, and it handles the rest. You then can create classes to encapsulate your information and simply persist instances of them to the file through db4o. You can ask for them later through a very simple query interface.

Travis Heseman
Excellent thanks. Is Db4o the most widely/popular choice used in it's category?
Greg
If by category you're referring to C#/Java object databases, then yes I believe so. However, if you mean storing small primitive data from one session to another, then using Settings, a custom ConfigurationSection, or another XML file is most widely used.If you find you need more database features but don't want to have to manage an RDBMS, the object database is something worth looking into.
Travis Heseman
db4o seems to require a license if used for commercial purposes it seems? site seems to suggest you need to ask for a quote (no figures mentioned)...
Greg
db4o does require licensing for specific commercial uses. However, a free license can be obtained and used under GNU GPL, Version 2. You'll need to consider your purpose and see if your use is permissible under that licensing agreement. http://www.db4o.com/about/company/legalpolicies/gpl.aspx
Travis Heseman