且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#表单保存并加载数据

更新时间:2023-02-02 18:33:52

这实际上非常简单,假设您的文本框都是单行。 (如果它们是多行的,它会变得有点复杂,因为您可能需要将它们转换为带分隔符的单个字符串,并在重新加载时将它们重新分开)



有很多方法可以做到这一点。

1)你可以从文本文件中读取和写入 - 这很简单:只需创建一个StreamWriter并编写每个字符串在另一条线上。然后逐行阅读它们并重置文本框等等。

2)你可以使用应用程序设置 - 再次简单,你在应用程序中添加一个设置然后访问它属性每行的.Settings.Default.MySettingName。如果文件最初不存在,那么优于文本文件可以提供默认值。

3)您可以使用XMLDocument来序列化数据。更复杂,但不是很多。

4)您可以使用一个简单的数据库(可能是SQLCE或SQLite)来存储和重新提供数据。



还有其他解决方案(很多)但这些可能是最简单的解决方案。



这取决于你想要存储的东西,以及你想要的东西是多么健壮/可编辑。



对于一个没有使用文件名的非常简单的解决方案,我可能会选择2。
This is actually pretty simple, assuming that your textboxes are all single line. (If they are multiline, it gets a little more complex as you probably need to convert them to a single string with separators, and split them back up again when you reload them)

There are a load of ways to do this.
1) You could just read and write from a text file - it''s simple to do: just create a StreamWriter and write each string on a separate line. Then read them back line by line and reset you textboxes and so forth.
2) You could use applications settings - again it''s simple, you add a setting in your application then access it vis Properties.Settings.Default.MySettingName for each line. Has the advantage over a text file that you can supply a default if the file is not initially present.
3) You could use an XMLDocument to serialise your data. A little more complex, but not a lot.
4) You could use a simple database (SQLCE or SQLite perhaps) to store and retirve your data.

There are other solutions (lot of them) but those are probably the simplest ones to implement.

It depends on what you want to store really, and how robust / human editable you want things to be.

For a really simple solution that didn''t use file names, I''d probably go with option 2.


引用:

我试图找到一种将表单数据保存到某些内容的简单方法文本文件并从中读取,但....找不到任何有用的东西...

I was trying to find out a simple way of saving form data into some text file and reading from it, but.... could not find anything usefull...



这根本不是真的: Google [ ^ ]