7

I am trying to use an ArrayList to store a variable number of strings, and would like to know how to save the ArrayList and its elements so that my windows form can recall their value between program load and exit.

I used to store the information in a text file, but would like to avoid external files if possible.

Thank you for any help you could provide.

user112016322
  • 688
  • 3
  • 11
  • 26
  • 1
    how about an embedded database? (however i find that quite the overkill for just 1 array) – Yoeri Jul 30 '12 at 12:57
  • 4
    You have to store the information *somewhere* external to your application, so where is your preferred storage? File, registry or database are the likely options - you'd have to specify which if you want a useful answer. – Dan Puzey Jul 30 '12 at 12:57
  • 1
    suppose file will be the best approach, cuz you probably will have problems with access to registry, DB will be overkill as it was said – Artiom Jul 30 '12 at 12:57
  • 3
    Not directly related to your question, but pretty please, use generic collections (e.g. `List`), not `ArrayList`. – svick Jul 30 '12 at 12:57
  • I think that Windows Registry is a nice idea, too. Take a look here: http://www.c-sharpcorner.com/uploadfile/puranindia/the-windows-registry-in-C-Sharp/ Another nice article is published there: http://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C – Ionică Bizău Jul 30 '12 at 13:01

4 Answers4

7

You can save the ArrayList (if not ArrayList their are other equivalent classes) using Properties.Settings best part is it allows you the setting variable at Application and user level

A very good example can be found here how to use Settigns http://www.codeproject.com/Articles/17659/How-To-Use-the-Settings-Class-in-C

HatSoft
  • 11,077
  • 3
  • 28
  • 43
  • @user1197993 Your welcome and will be happy to help if you are stuck – HatSoft Jul 30 '12 at 13:03
  • This will create an external file for the user. Depending on details of code signing this could be different for every version of the application: saved data will be lost on an upgrade. – Richard Jul 30 '12 at 13:15
  • @Richard on the properties window the BuildAction for settings can turned in to Emebedded Resource – HatSoft Jul 30 '12 at 13:19
  • HatSoft, I put variable str as a string type in the settings and then i try to say string str1 = Properties.Settings.str but then it says its a 'field' but its being used as a 'type'. What could be the issue? – user112016322 Jul 30 '12 at 19:44
  • @user1197993 you need to use it this way Properties.Settings.Default.str1 – HatSoft Jul 30 '12 at 19:58
2

I've always done using (In Winforms in your case from the sounds of it), the Form_Closing to store to a Properties.Settings variable you'd create beforehand. If it's an ArrayList, you could store it to XML or a comma separated list. Your serizliation/deserialization method will depend on your data.

Echilon
  • 10,064
  • 33
  • 131
  • 217
1

Have a look at isolated storage.

svick
  • 236,525
  • 50
  • 385
  • 514
Yoeri
  • 2,249
  • 18
  • 33
1

I used to store the information in a text file, but would like to avoid external files if possible.

Inevitably storing data between runs will require something outside the program's executables.

The registry would work, but the registry is not great for storing anything more than a small amount of information. A database could be used by that adds files.

For text strings a text file – one string per line1 – can be saved and loaded in a single statement. Putting the file into isolated storage or under a dedicated folder in %AppData% limits the chances of a user messing it up.2.

// Load
var theStrings = new ArrayList();
var path = GetSavePath();
if (File.Exists(path)) {
  theStrings.AddRange(File.ReadLines(path);
}

// Save:
File.WriteAllLines(GetSavePath(), theStrings.ToArray());

Here using ToArray() as ArrayList doesn't implement IEnumerable<String> (List<String> would be a better choice for a collection and avoid this).


1 This assumes end of line is not valid inside the strings. If this needs to be supported there are a number of options. Some file format to separate the strings by another mechanism, or perhaps the easiest will be to escape characters with a simple transform (eg. \\\, newline → \n, and carriage return → \r).

2 You cannot prevent this without significant additional complexity that would use something like a service to load/save as a different user thus allowing the data to be protected by an ACL.

Richard
  • 106,783
  • 21
  • 203
  • 265