0

I am making a video Converter in c# on windows Framework. i want to save loaded files that user add & if it Exit the Application i want to store that files and load when it runs application again. How to do this. ?? Give me idea about how to do this.

Hafiz Ali
  • 3
  • 5
  • 1
    Possible duplicate of [saving state between program restarts](http://stackoverflow.com/questions/7522228/saving-state-between-program-restarts) – Sinatr Jul 15 '16 at 11:25

3 Answers3

0

Using the settings api may help: http://geekswithblogs.net/mbcrump/archive/2010/06/17/configuring-applicationuser-settings-in-wpf-the-easy-way.aspx

LoekD
  • 11,402
  • 17
  • 27
0

You need to use user settings. Open the project properties in Visual Studio and click the "Settings" tab. You can add a new user setting there of type System.Collections.Specialized.StringCollection by entering a setting name, selecting the type System.Collections.Specialized.StringCollection from the combo box and then defining it as a user scope setting.

If you forget to make it a user setting and create an application setting instead, it will be read-only and you have no chance of changing it. Only user settings can be changed.

This will look to your code like a normal list of strings. You can add a new value to the like and save it like this:

Properties.Settings.Default.[Setting name you chose].Add("My String");
Properties.Settings.Default.Save();

Of course you can also loop over the entries like:

foreach (string s in Properties.Settings.Default.[Setting name you chose])
    ....

The advantage is that you don't need to care about where the settings are stored and how. .NET does all that for you.


Background: Creating settings will add them to the app.config file. This file will be copied into the output directory when you compile as [exename].exe.config. When the application starts, the framework will read the application settings from there. The user settings will either be read from there or (if you changed and saved them) from a copy that resides somewhere in the user's application data directory.

Do not, I repeat: do not attempt to modify that .exe.config file directly from your code, but always use the properties that are automatically generated for you as I described above.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • I generally find that the string collection is more hassle than help, i doesn't implement IEnumerable which means its not compatible directly with some of the more useful Linq extensions and causes quite a bit of bloat in your settings file. I'd only recommend it when you are storing complex strings that are hard to split, otherwise just use a string – MikeT Jul 15 '16 at 12:36
  • Yes, but for the OP's purpose it is perfect. He needs to store a list of files (I guess: file names?). Probably he has a list of files internally anyway, so re-loading these files is a simple for-loop. I doubt that being able to use LINQ outweighs the pain of splitting up a string that contains all the file names in this case... Also, while LINQ may be a nice tool, we were able to program without it only 5 years ago. You can still write loops, you know? But that's a different discussion :-) As for the bloated settings file: Who cares whether the file is 1K or 2K or even 10K? – Thorsten Dittmar Jul 15 '16 at 12:39
-1

You could create a Settings class which contains a list of the files previously loaded and then on Exit serialize it to disk and on start deserialize it.

Thomas Hahn
  • 332
  • 4
  • 13