I used to store information in a text file, but would like to avoid external files if possible.
Inevitably storing data between runs, requires something outside of the executable programs.
The registry will work, but the registry is not very good for storing anything more than a small amount of information. A database can be used to add files.
For text lines, a text file โ one line in line 1 โ can be saved and loaded into a single statement. Placing a file in isolated storage or in a dedicated folder in% AppData% limits the user's chances of ruining it. 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 an ArrayList does not implement IEnumerable<String> ( List<String> would be the best choice for the collection and avoid this).
1 This assumes that the end of the line is not valid inside the lines. If necessary for support, there are a number of options. Some file format for separating lines with another mechanism, or perhaps the easiest, will be to avoid characters with a simple conversion (for example. \ โ \\ , newline โ \n , and carriage return โ \r ).
2 You cannot prevent this without significant additional complexity, which will use something like a service to load / save as another user, which allows you to protect ACL data.
source share