C# 如何在程序退出时保存变量值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11722040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save variable values on program exit?
提问by user112016322
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.
我正在尝试使用 ArrayList 来存储可变数量的字符串,并且想知道如何保存 ArrayList 及其元素,以便我的 windows 窗体可以在程序加载和退出之间调用它们的值。
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.
感谢您提供的任何帮助。
采纳答案by HatSoft
You can save the ArrayList (if not ArrayList their are other equivalent classes) using Properties.Settingsbest part is it allows you the setting variable at Application and user level
您可以使用Properties.Settings保存 ArrayList(如果不是 ArrayList 它们是其他等效类),最好的部分是它允许您在应用程序和用户级别设置变量
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
一个很好的例子可以在这里找到如何使用 Settigns http://www.codeproject.com/Articles/17659/How-To-Use-the-Settings-Class-in-C
回答by Yoeri
Have a look at isolated storage.
看看隔离存储。
回答by Echilon
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.
我一直使用(在 Winforms 中,从它的声音来看),将 Form_Closing 存储到您预先创建的 Properties.Settings 变量中。如果它是一个 ArrayList,您可以将它存储到 XML 或逗号分隔的列表中。您的序列化/反序列化方法将取决于您的数据。
回答by Richard
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.
对于文本字符串,可以在单个语句中保存和加载文本文件(每行1个字符串)。将文件放入独立存储或 %AppData% 中的专用文件夹下可以限制用户将其弄乱的机会。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 ArrayListdoesn't implement IEnumerable<String>(List<String>would be a better choice for a collection and avoid this).
这里使用ToArray()asArrayList没有实现IEnumerable<String>(List<String>对于集合来说是更好的选择并避免这种情况)。
1This 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).
1这假设行尾在字符串内无效。如果需要支持,有多种选择。某些文件格式通过另一种机制分隔字符串,或者最简单的方法是使用简单的转换(例如\→ \\、换行 →\n和回车 → \r)来转义字符。
2You 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.
2如果没有显着的额外复杂性,您就无法防止这种情况发生,这种复杂性会使用诸如服务之类的东西以不同用户的身份加载/保存,从而允许数据受 ACL 保护。

