persistantly在C#中存储设置/变量/。NET [英] Storing settings/variables persistantly in C#/.NET

查看:538
本文介绍了persistantly在C#中存储设置/变量/。NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是一个总福利局问题,所以请大家见谅。

Ok, this is a total newb question, so please forgive me.

什么是存储变量,使他们持续存在,恢复最好的方法是什么?我有一个使用约10个变量(字符串和十进制)的设置一个小应用程序。目前,我他们全部转换为字符串,如果需要的话,把它们放到一个数组和序列化数组到一个文件中。

What is the best way to store variables so that they persist and are recoverable? I have a small application that uses about 10 variables (string and decimal) as settings. Currently, I convert them all to strings, if needed, put them into an array and serialize the array to a file.

这是一个痛苦,如果我曾经想添加一个变量。所以,我想使用一个哈希表和序列化的。同样,不知道什么是做到这一点的最好办法。

This is a pain if I ever want to add a variable. So, I was thinking about using a hashtable and serializing that. Again, not sure what is the best way to do this.

有些要求我必须是需要将数据安全地(加密)保存,并且必须由其他应用程序(我有读取设置两个小应用程序)。

Some requirements I have are that the data needs to be stored securely (encrypted), and must be accessible by other applications (I have two other small apps that read the settings).

我知道我是过于复杂的东西所以基本和简单。这必须在几乎所有的内置应用程序来完成。

I know I am over-complicating something so basic and simple. This must be done in nearly every application built.

TIA

推荐答案

在simpliest,有效和最灵活的方法是在需要时创建一个类,添加设置,然后序列化/反序列化。此源$ C ​​$ C的类可以在其他组件中重复使用,和持久性可以在任何地方。确保该类知道如何序列化/反序列化的,因为你的安全需求本身。这确保了实现保持与类。然后调用程序集只需要通过调用静态/共享方法来创建对象。

The simpliest, effective and most flexible approach is to create a class, add settings, then serialize/deserialize when needed. This source code for the class can be reused in other assemblies, and persistence can be anywhere. Make sure this class knows how to serialize/deserialize itself because of your security requirement. This ensures the implementation stays with the class. Then the calling assembly just needs to create the object by calling a static/shared method.

此为您提供了强类型的设置,版本控制,能够添加新的设置,甚至复杂的数据类型(其他类)。这个对象甚至可以传递到其他对象作为参数,并且因为它支持序列,这是非常灵活的。

This gives you strongly-typed settings, versioning, ability to add new settings, and even complex data types (other classes). This object can even be passed to other objects as arguments, and since it supports serialization, it is very flexible.

请参阅操作方法(对象类=>二进制序列化=>为内存=>加密=>保存到文件)的的<一个href="http://social.msdn.microsoft.com/forums/en-US/netfxremoting/thread/68c200c2-4aa4-48dc-95be-6fe077fd10f4/" rel="nofollow">http://social.msdn.microsoft.com/forums/en-US/netfxremoting/thread/68c200c2-4aa4-48dc-95be-6fe077fd10f4/

ISerializable接口的的的http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx

隔离储存的在 HTTP:/ /msdn.microsoft.com/en-us/library/3ak841sy.aspx

这篇关于persistantly在C#中存储设置/变量/。NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆