C# 存储用户输入的数据

发布于 2024-07-12 05:18:23 字数 421 浏览 11 评论 0原文

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

我正在使用 Visual C# Express。 在不需要某种数据库(例如 MySQL、MSSQL 或 Access)的情况下执行此操作的最佳方法是什么?

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

ps

我忘了提及,我希望用户无法轻松访问此文件并读取内容。 没有加密的文本文件很容易打开。 有什么方法可以加密这个吗? 另外,如果我使用像“:”这样的分隔符,那么这意味着用户不能使用该字符。 那么还有其他办法吗?

谢谢!

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.

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?

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?

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里人 2024-07-19 05:18:23

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

[Serializable]

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

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

保存:

    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;

    }

加载:

    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;
    }

您还可以在流上使用“using”,但我认为这段代码非常简单。 这也意味着您可以将更多项目添加到 MyObject 中。

编辑:对于加密,您可以添加 AES 或类似的东西。 这对您来说可能有点过分了,将文件保存为二进制可能会使它可以被记事本之类的东西读取,但不容易编辑。 以下是有关真正加密的详细说明:

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

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.

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:

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

度的依靠╰つ 2024-07-19 05:18:23

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

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.

还不是爱你 2024-07-19 05:18:23

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

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

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

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)

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.

(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)

围归者 2024-07-19 05:18:23

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文