C#存储用户输入的数据

时间:2022-01-15 07:01:15

I'm trying to write a C# program, where when a user enters some data into a text box and clicks on a "save" button, the information is stored in some sort of file that when the program is opened the next time around, the information is automatically loaded in.

我正在尝试编写一个C#程序,当用户在文本框中输入一些数据并单击“保存”按钮时,信息存储在某种文件中,当下次打开程序时,信息会自动加载。

I'm using Visual C# express. What's the best way to do this without requiring some sort of database like MySQL or MSSQL or Access?

我正在使用Visual C#express。在不需要某种数据库(如MySQL或MSSQL或Access)的情况下,最好的方法是什么?

If the only way or easiest way is a database, I'd rather use Access. If so, does the user of the program need Access installed for my program to run on their computer? What if I go with another database?

如果唯一的方法或最简单的方法是数据库,我宁愿使用Access。如果是这样,程序的用户是否需要为我的程序安装Access才能在他们的计算机上运行?如果我使用其他数据库怎么办?

p.s.

I forgot to mention, I'd rather the user not be able to access this file and read the contents easily. Text file without encryption would be easy to open. Any way to encrypt this? Also, if I use a delimiter like ':', then that means the user cannot use that character. So any other way?

我忘了提及,我宁愿用户无法访问此文件并轻松阅读内容。没有加密的文本文件很容易打开。有什么办法加密吗?此外,如果我使用像':'这样的分隔符,那么这意味着用户无法使用该字符。那么还有其他方式吗?

Thanks!

4 个解决方案

#1


Make your user data serializable by adding the keyword:

通过添加关键字使您的用户数据可序列化:

[Serializable]

above your data structure. When you load the dialog box, load your serialized structure from disk, and when you leave the dialog, save the data structure.

在您的数据结构之上。加载对话框时,从磁盘加载序列化结构,当您离开对话框时,保存数据结构。

From a style standpoint, you should probably not have the dialog box change data until the dialog box is closed (if it's modal).

从样式的角度来看,在对话框关闭之前,您可能不应该让对话框更改数据(如果它是模态的)。

To save:

    private bool Save(String inFileName, MyObject inObject){
        try {
            FileStream theStream = File.Open(inFileName, FileMode.Create);

            BinaryFormatter theFormatter = new BinaryFormatter();
            theFormatter.Serialize(theStream, inObject);//add it to the end there
            theStream.Dispose();
            theStream.Close();
        } catch{
            return false;
        }
        return true;

    }

To Load:

    private MyObject Read(String inFileName){
        MyObject theReturn = null;
        try {

            FileStream theStream = File.Open(inFileName, FileMode.Open, FileAccess.Read);                

            BinaryFormatter theFormatter = new BinaryFormatter();
            theReturn = (CImageData)theFormatter.Deserialize(theStream);//add it to the end there
            theStream.Dispose();
            theStream.Close();
        }
        catch {
            return null;
        }
        return theReturn;
    }

You can also use 'using' on a stream, but this code is pretty straightforward, I think. It also means that you can add more items into MyObject.

你也可以在流上使用'using',但我认为这段代码非常简单。它还意味着您可以向MyObject添加更多项目。

Edit: For encryption, you can add in AES or something similar. That might be overkill for you, and saving the file as binary may make it readable by something like notepad, but not easily editable. Here's a lengthy explanation on real encryption:

编辑:对于加密,您可以添加AES或类似的东西。这可能对您来说太过分了,将文件保存为二进制文件可能会使记事本之类的内容可读,但不容易编辑。这是对真实加密的冗长解释:

http://msdn.microsoft.com/en-us/magazine/cc164055.aspx

#2


Sql Compact Edition would hide the data from being easily accessible (and its free). You can also password protect a CE database. A SQL CE database is contained completely in an .SDF file, like Access, so you can copy the .sdf file around and not have to worry about network connectivity, etc.

Sql Compact Edition会隐藏数据,使其不易被访问(并且是免费的)。您还可以使用密码保护CE数据库。 SQL CE数据库完全包含在.SDF文件中,如Access,因此您可以复制.sdf文件,而不必担心网络连接等。

#3


If your application is only to be used by a single person then simply store your data in a file (XML would give you some structure)

如果您的应用程序仅供一个人使用,那么只需将您的数据存储在一个文件中(XML会为您提供一些结构)

If your application will be used by multiple people and the data shared among them, then a database is your best option. If you use a database then yes you will need to have a database installed on your users computer, though it is possible to do that transparantly to your user, for that you are best off with an embeddable database, something like MySQL would be your best bet.

如果您的应用程序将由多人使用并且他们之间共享数据,那么数据库是您的最佳选择。如果您使用数据库然后是,您将需要在您的用户计算机上安装数据库,尽管可以透明地对您的用户执行此操作,因为您最好使用可嵌入的数据库,像MySQL这样的东西是您最好的打赌。

(EDIT: the database actually does not have to be on the users computer, but he would need to be able to see it from his coputer)

(编辑:数据库实际上不必在用户计算机上,但他需要能够从他的计算机上看到它)

#4


If it's very simple then you could place it in a plain text file. For more structured data you could consider storing it as a CSV and parsing it or creating an XmlDocument and saving that to disk.

如果它非常简单,那么您可以将它放在纯文本文件中。对于更多结构化数据,您可以考虑将其存储为CSV并对其进行解析或创建XmlDocument并将其保存到磁盘。

#1


Make your user data serializable by adding the keyword:

通过添加关键字使您的用户数据可序列化:

[Serializable]

above your data structure. When you load the dialog box, load your serialized structure from disk, and when you leave the dialog, save the data structure.

在您的数据结构之上。加载对话框时,从磁盘加载序列化结构,当您离开对话框时,保存数据结构。

From a style standpoint, you should probably not have the dialog box change data until the dialog box is closed (if it's modal).

从样式的角度来看,在对话框关闭之前,您可能不应该让对话框更改数据(如果它是模态的)。

To save:

    private bool Save(String inFileName, MyObject inObject){
        try {
            FileStream theStream = File.Open(inFileName, FileMode.Create);

            BinaryFormatter theFormatter = new BinaryFormatter();
            theFormatter.Serialize(theStream, inObject);//add it to the end there
            theStream.Dispose();
            theStream.Close();
        } catch{
            return false;
        }
        return true;

    }

To Load:

    private MyObject Read(String inFileName){
        MyObject theReturn = null;
        try {

            FileStream theStream = File.Open(inFileName, FileMode.Open, FileAccess.Read);                

            BinaryFormatter theFormatter = new BinaryFormatter();
            theReturn = (CImageData)theFormatter.Deserialize(theStream);//add it to the end there
            theStream.Dispose();
            theStream.Close();
        }
        catch {
            return null;
        }
        return theReturn;
    }

You can also use 'using' on a stream, but this code is pretty straightforward, I think. It also means that you can add more items into MyObject.

你也可以在流上使用'using',但我认为这段代码非常简单。它还意味着您可以向MyObject添加更多项目。

Edit: For encryption, you can add in AES or something similar. That might be overkill for you, and saving the file as binary may make it readable by something like notepad, but not easily editable. Here's a lengthy explanation on real encryption:

编辑:对于加密,您可以添加AES或类似的东西。这可能对您来说太过分了,将文件保存为二进制文件可能会使记事本之类的内容可读,但不容易编辑。这是对真实加密的冗长解释:

http://msdn.microsoft.com/en-us/magazine/cc164055.aspx

#2


Sql Compact Edition would hide the data from being easily accessible (and its free). You can also password protect a CE database. A SQL CE database is contained completely in an .SDF file, like Access, so you can copy the .sdf file around and not have to worry about network connectivity, etc.

Sql Compact Edition会隐藏数据,使其不易被访问(并且是免费的)。您还可以使用密码保护CE数据库。 SQL CE数据库完全包含在.SDF文件中,如Access,因此您可以复制.sdf文件,而不必担心网络连接等。

#3


If your application is only to be used by a single person then simply store your data in a file (XML would give you some structure)

如果您的应用程序仅供一个人使用,那么只需将您的数据存储在一个文件中(XML会为您提供一些结构)

If your application will be used by multiple people and the data shared among them, then a database is your best option. If you use a database then yes you will need to have a database installed on your users computer, though it is possible to do that transparantly to your user, for that you are best off with an embeddable database, something like MySQL would be your best bet.

如果您的应用程序将由多人使用并且他们之间共享数据,那么数据库是您的最佳选择。如果您使用数据库然后是,您将需要在您的用户计算机上安装数据库,尽管可以透明地对您的用户执行此操作,因为您最好使用可嵌入的数据库,像MySQL这样的东西是您最好的打赌。

(EDIT: the database actually does not have to be on the users computer, but he would need to be able to see it from his coputer)

(编辑:数据库实际上不必在用户计算机上,但他需要能够从他的计算机上看到它)

#4


If it's very simple then you could place it in a plain text file. For more structured data you could consider storing it as a CSV and parsing it or creating an XmlDocument and saving that to disk.

如果它非常简单,那么您可以将它放在纯文本文件中。对于更多结构化数据,您可以考虑将其存储为CSV并对其进行解析或创建XmlDocument并将其保存到磁盘。