应用程序执行之间preserve数据应用程序、数据、preserve

2023-09-03 11:25:39 作者:倾听&夏末

我有一些文本框的应用程序。我的用户填写文本框和运行一些方法中,当它们关闭应用程序数据丢失(通常)。

I have an application with some textboxes. My user fills the textboxes and runs some methods, when they close the application data is lost (normally).

我想保留一对夫妇文本框和一些局部变量的值。这是不值得使用数据库,和简单的 .TXT 文件是不够干净,有没有其他的简单,存储的数据量小应用程序运行之间的简单方法是什么?

I want to keep the value of a couple of textboxes and some local variables. It's not worth it to use database, and simple .txt files are not clean enough, is there any other simple and brief way of storing little volumes of data between application runs?

我不知道,但听说过的资源文件的一些缕缕,都有益于这种情况?

I'm not sure but have heard some wisps about resource files, are they good for this case?

推荐答案

简单的方法是结合你的文本框来应用程序设置:

Simplest way is binding your textboxes to application settings:

选择texbox要preserve 转到属性>数据>(的applicationSettings) 添加应用程序设置绑定到Text属性 在 FormClosed 事件保存应用程序设置 select texbox you want to preserve go to Properties > Data > (ApplicationSettings) add application settings binding to Text property on FormClosed event save application settings

保存设置:

private void Form_FormClosed(object sender, FormClosedEventArgs e)
{
    Settings.Default.Save();
}

下一次,当用户启动应用程序,设置将被从用户特定的文件加载,和文本框将充满相同的数据,因为它是之前用户关闭应用程序最后一次了。

Next time when user will start your application, settings will be loaded from user-specific file, and textboxes will be filled with same data as it was before user closed an application last time.

此外,在应用程序设置可以存储局部变量,但是你必须手动添加的设置对他们来说,并手动读取应用程序启动设置:

Also in application settings you can store local variables, but you will have to add settings for them manually, and manually read that setting on application start:

开启属性下的项目文件夹> Settings.settings 要存储添加设置(如MyCounter) 设置MyCounter类型,范围和默认值(例如int,用户,0) 阅读设置到本地变量变种X = Settings.Default.MyCounter 在形成封闭保存设置 Settings.Default.MyCounter = X 之前调用 Settings.Default.Save() open Properties folder under project > Settings.settings add settings you want to store (e.g. MyCounter) set MyCounter type, scope, and default value (e.g. int, User, 0) read setting to your local variable var x = Settings.Default.MyCounter on form closed save setting Settings.Default.MyCounter = x just before calling Settings.Default.Save()