WinForms - which is easiest approach for persisting some data?

前端 未结 6 1428
南笙
南笙 2020-12-31 19:34

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 wit

相关标签:
6条回答
  • 2020-12-31 19:52

    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.

    0 讨论(0)
  • 2020-12-31 19:52

    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

    0 讨论(0)
  • 2020-12-31 19:55

    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.

    0 讨论(0)
  • 2020-12-31 19:58

    Depends on how much data you want to store but if it is just a small set of values (rather than a set of objects or records) then also look into Isolated Storage:

    https://msdn.microsoft.com/en-us/library/3ak841sy(v=vs.110).aspx

    0 讨论(0)
  • 2020-12-31 20:02

    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.

    0 讨论(0)
  • 2020-12-31 20:03

    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.

    0 讨论(0)
提交回复
热议问题