简体   繁体   English

如何以可移植的形式在c#应用程序中存储数据?

[英]how to store data in c# application in a portable form?

I'm writing an application using windows form and c# 3.0. 我正在使用Windows窗体和c#3.0编写应用程序。 I was wondering if there is a recommended way of persist data across time. 我想知道是否存在一种建议的跨时间持久化数据的方法。 However, i do not want to touch the machine it is running on, as a result, i would like to store the data in the binary executable (preferably, due to the need not clutter up the user's folder with random config files). 但是,我不想触摸运行它的机器,因此,我想将数据存储在二进制可执行文件中(最好是,由于不需要用随机的配置文件来整理用户的文件夹)。

So if anyone have any ideas of how to do this, it would be much appreciated! 因此,如果有人对如何执行此操作有任何想法,将不胜感激!

Jason 杰森

If you're looking to store configuration information - app.config or a settings file is probably the way to go. 如果您要存储配置信息 -可能要使用app.config或设置文件。

If you are storing user data - you should really allow the user to control where it is saved - and prefer the \\User\\Username folder on the machine. 如果要存储用户数据 -您应该确实允许用户控制数据的保存位置-并且更喜欢计算机上的\\ User \\ Username文件夹。

As for what format to store it in ... you can certainly use something like SQLLite - but there's nothing wrong with XML either, if you're not storing true binary data. 至于将其存储为哪种格式,您当然可以使用类似SQLLite的格式 -但是,如果您不存储真正的二进制数据,XML也没有错。 .NET offers a number of APIs to transform object graphs into XML representations - which you may want to look into. .NET提供了许多API,可以将对象图转换为XML表示形式,您可能需要研究一下。

If you don't want to store anything on the local user's machine, you probably want a network database - or a webservice - to which you upload the users data. 如果您不想在本地用户的计算机上存储任何内容 ,则可能需要将用户数据上传到的网络数据库或Web服务。 Just make sure your users understand this - many don't like their private data being sent somewhere on the web without their consent. 只需确保您的用户了解这一点-许多人不喜欢未经他们同意将其私人数据发送到网络上的某个地方。

You really don't want to go about modifying the executable file. 您确实不想修改可执行文件。 Many virus scanners quarantine executables that are constantly changing in content or size - as a way to proactively prevent viruses and malware from infecting the machine. 许多病毒扫描程序会隔离内容或大小不断变化的可执行文件,以主动预防病毒和恶意软件感染计算机。 You don't want to go there. 你不想去那里。

Do not modify the executable. 不要修改可执行文件。 Adding a single SQLite database is a much better solution. 添加单个SQLite数据库是一个更好的解决方案。

Isolated storage is another alternative. 隔离存储是另一种选择。

  • Doesn't clutter install directory 不混乱安装目录
  • Doesn't cause issues with AnitVirus software 不会引起AnitVirus软件问题
  • Part of the OS including .Net objects, don't need to install anything else 操作系统的一部分,包括.Net对象,不需要安装其他任何东西
  • Already works with the Windows security model 已经适用于Windows安全模型
  • Exists on a per user basis, so saved settings are separated for each user 基于每个用户存在,因此为每个用户分开保存的设置
  • Can serialize/deserialize obects directly into it 可以直接将对象序列化/反序列化

SQLite是您所寻找的并且与c#兼容

如果您不想将数据存储在最终用户PC上的SQLite数据库中,则可以调出另一台服务器上的Web服务,该服务器将其数据存储在SQL Server或其他内容中。

I don't believe a windows form project can modify itself like that (I've tried to find a way to do this myself some time ago). 我不相信Windows窗体项目可以像这样修改自身(一段时间前,我试图自己找到一种方法)。 Some form of hosted application such as a silverlight application (where the application is essentially a zip file) may be the way to go. 某种形式的托管应用程序,例如silverlight应用程序(其中的应用程序本质上是zip文件)可能是解决方法。 Silverlight applications would require the silverlight plugin though (and I'm still not sure if a silverlight application is allowed to modify itself). Silverlight应用程序将需要Silverlight插件(并且我仍然不确定是否允许Silverlight应用程序修改自身)。

I would think that one config file of some sort would be prefereable, and not leave much clutter. 我认为某个配置文件是可取的,并且不会造成太多混乱。

One way to ensure that your applicaiton is entirely self contained would be to use a program like ThinStall after you have compiled the project. 确保您的应用程序完全独立的一种方法是在编译项目后使用诸如ThinStall之类的程序。 This virtualises the application and could give it it's own file system or registry internally to the .exe file. 这会将应用程序虚拟化,并可以在.exe文件内部为其提供自己的文件系统或注册表。

One way for an executable to change itself would be to put another executable inside it, (embed as a resource then extract it to a file when needed). 可执行文件更改自身的一种方法是将另一个可执行文件放入其中((作为资源嵌入,然后在需要时将其提取到文件中))。 This executable could then modify the first, however I don't think ther'es any framework for it to do that, so it would require knowing excatly what to change and where. 然后,该可执行文件可以修改第一个可执行文件,但是我认为没有任何可执行文件可以执行此操作,因此它需要完全了解要更改的内容和更改位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM