C# 在用户会话之间保留对象而不使用数据库

发布于 2024-10-15 06:48:54 字数 154 浏览 9 评论 0原文

我正在制作一个简单的 Winform,它读取文本文件并进行一些解析以将其放入 C# 对象中,然后由我的应用程序进行操作。

我想解析一次文件。然后离开一周,回来并将所有对象加载回我的应用程序中。

我不想持久化到数据库,因此希望有某种我可以连接的简单对象持久化框架。

I'm making a simple Winforms that reads in a text file and does some parsing to put it into C# objects that are then manipulated by my app.

I would like to parse the file once. Then go away for a week, come back and load all the objects back into my app.

I don't want to persist to database thus was hoping there was some sort of simple object persistance framework I could hook into.

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

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

发布评论

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

评论(7

丑疤怪 2024-10-22 06:48:54

您正在谈论序列化

查看 DataContractSerializerDataContractJsonSerializer

您只需添加 DataContract< /a> 和 DataMember< /a> 属性告诉序列化器如何管理序列化。

如果您的对象不会更改(新/旧/重命名的属性和可见性)并且您不介意阅读序列化格式,则可以尝试 BinaryFormatter


框架中有几个较旧的序列化器(XmlSerializerBinaryFormatter 等),但对序列化格式的最佳控制是使用上面的序列化器。

You are talking about serialization.

Look into the DataContractSerializer for this. If you want a file that isn't XML you can try the DataContractJsonSerializer.

You simply add the DataContract and DataMember attributes to tell the serializer how to manage the serialization.

If your objects will not change (new/old/renamed properties and visibility) and you don't care to read the serialization format, you can try the BinaryFormatter.


There are several older serializers in the frame work (XmlSerializer, BinaryFormatter and more), but the best control over the serialization format will be with the above).

无法回应 2024-10-22 06:48:54

您可以使用应用程序设置或用户设置。使用可以在项目属性下找到的向导非常容易。 这里是教程。

You could use application settings or user settings. Its pretty easy with the wizard that can be found under the projects properties. here is the tutorial for it.

绝不服输 2024-10-22 06:48:54

您可以将它们序列化为 XML 文件。简单、快速、容易(除非有人破解了文件)。

You could just serialize them to an XML file. Simple, quick and easy (unless someone hacks the file).

树深时见影 2024-10-22 06:48:54

存储对象状态的最简单的非数据库方法是序列化。将您的类标记为 Serialized 或 DataContract,然后使用 BinaryFormatter(如果使用 DataContract,则使用 DataContractSerializer)将对象转换为可持久状态。然后可以将其保存到一个文件中,您可以在应用程序启动备份时加载该文件并以类似的方式反序列化。请注意,类的成员(包括子类)必须以相同的方式可序列化,并且您将丢失对非序列化元素(例如指针和委托引用)的任何引用。

The simplest non-DB method of storing object state is serialization. Mark your class as either Serializable or a DataContract, then use a BinaryFormatter (or DataContractSerializer if you used the DataContract) to convert the object into a persistable state. That can then be saved to a file that you can load when the app starts back up and deserialize in a similar fashion. Be aware that the class's members, including child classes, must be serializable in the same way, and that you will lose any references to non-serialized elements such as pointers and delegate references.

披肩女神 2024-10-22 06:48:54

您可以按照之前的建议使用序列化。

保存数据的好地方是 Windows 的 LocalData 文件夹 -

Environment.SpecialFolder.LocalApplicationData

You can use serialization as sugested before.

A good place to save the data in is Windows's LocalData folder -

Environment.SpecialFolder.LocalApplicationData
我早已燃尽 2024-10-22 06:48:54

最简单的方法可能是使用 BinaryFormatter 类将对象直接序列化到文件中。您需要确保所有类型都标记为 [Serializable],但这种方法既快速又简单。

以下是任意对象图的“读取”和“写入”方法的简单示例:

private void SerializeToFile(string fileName, object o)
{
    var binaryFormatter = new BinaryFormatter();
    using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        binaryFormatter.Serialize(fileStream, o);
    }
}

private object DeserializeFromFile(string fileName)
{
    var binaryFormatter = new BinaryFormatter();
    using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
    {
        return binaryFormatter.Deserialize(fileStream);
    }
}

其他方法包括 XmlSerialization(XmlSerializer 类) - 如果您希望序列化对象易于人类读取,则更好或可编辑,或使用较新的 DataContractSerializer

Probably the simplest way to do this would be to serialize the objects directly to a file, using the BinaryFormatter class. You'll need to make sure that all your types are marked as [Serializable] but this approach is fast and easy.

Here is a simple example of a "read" and "write" method for an arbitrary object graph:

private void SerializeToFile(string fileName, object o)
{
    var binaryFormatter = new BinaryFormatter();
    using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        binaryFormatter.Serialize(fileStream, o);
    }
}

private object DeserializeFromFile(string fileName)
{
    var binaryFormatter = new BinaryFormatter();
    using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
    {
        return binaryFormatter.Deserialize(fileStream);
    }
}

Other approaches include XmlSerialization (the XmlSerializer class) - better if you want to the serialized objects to be human readable or editable, or using the newer DataContractSerializer.

君勿笑 2024-10-22 06:48:54

数据库出了什么问题?如果您不想拥有一个完整的引擎及其设置和相关问题,也许您可​​以看看基于“文件”的数据库...

我的第一个难点是:

  • SqlCompact
  • Access 数据库
  • SqlLite

...

取决于对象的复杂性坚持下去,不失为一个好的解决办法。

What is wrong with a database ? If you do not want to have a full engine with its setup and associated problems, maybe you can take a look at "file" based databases...

My first toughs are :

  • SqlCompact
  • Access databases
  • SqlLite

...

Depending the complexity of your objects to persist, it can be a good solution.

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