C# 中的数据源
长期读者,第一次海报。
背景:
我们用 C# 开发了一个小型应用程序,用于创建在我们的嵌入式系统中使用的专有二进制文件。该应用程序非常简单,只有几个文本框、组合框和复选框,以及几个用于创建文件的按钮。它仅作为 .exe 文件分发给我们的客户,这意味着不需要安装,这正是我们想要的。
问题:
我们的支持人员要求我实施一种保存某些设置并再次加载它们的方法。该数据应在应用程序运行之间保留。用户应该能够以不同的名称保存其中几个。
问题是:
我有什么选择?到目前为止,我已经确定了 3 种可能的解决方案:
- 纯文本文件:使用
- 设置文件很麻烦:Visual Studio 可以使用设置文件作为数据源,我已经尝试过这种方法,但被卡住了。也许不适合我?
- Microsoft SQL Server Compact:我对此了解甚少。我们可以将我们的部署方法与此解决方案一起使用吗?
Long time reader, first time poster.
Background:
We have developed a small application in C# for creating proprietary binary files for use in our embedded system. The application is very simple, just a few textboxes, comboboxes and checkboxes, and a couple of buttons for creating files. It is distributed to our customers as just an .exe file, meaning, no installation is required, and that is how we want it.
Problem:
I have been tasked by our support staff to implement a way to save some settings and load them again. This data should be retained in between runs of the app. The user should be able to save a couple of these under different names.
The question is:
What are my options? So far I have identified 3 possible solutions:
- Plain text files: Cumbersome to work with
- Settings file: Visual Studio can use a settings file as a datasource, I have tried this approach, but is stuck. Perhaps not for me?
- Microsoft SQL Server Compact: I know very little of this. Can we use our deployment method with this solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用Xml 文件来保存您的设置。您的应用程序可以加载这些设置并采取相应的操作。如果您使用.Net 4.0,您可以LINQ,这将使您的工作变得非常简单。请查看此。
You could use an Xml file to save your settings. Your application could load these settings and act accordingly. If you are .Net 4.0, you could LINQ that will make your job pretty easy. Have a look at this.
如果要保存的数据小而简单,那么我会选择物理文件;为应用程序设置 SQL Server Compact 的开销比仅仅将类或结构序列化为 XML 并保存到文件要大。
另一方面,如果数据变得更加复杂,包括一对多或多对多样式的关系,那么您可能需要转向数据库解决方案。
为了保持灵活性,请确保解耦保存/加载代码,以便在需要时可以从文件更改为数据库。
If the data to be saved is small and simple, then I would go for physical files; the overhead in setting up the SQL Server Compact for you application is larger than just serialising a class or struct to XML and saving to file.
On the other hand, if the data becomes more complicated, including one-to-many or many-to-many style of relationship, then you would want to move to the database solution.
To keep flexibility ensure you decouple the save/load code so you can change from file to database if the need arises.
“C# 设置”应该适合您。你试过对了吗?
请参阅以下链接。希望它有帮助
Visual Studio 设置文件 - 它是如何工作的?
http://msdn.microsoft.com/en-us/library/aa730869%28VS.80%29.aspx
它对我有用。
"C# Settings" should work for you. Have you tried it right?
Please see the below links. Hope it helps
Visual Studio Settings file - how does it work?
http://msdn.microsoft.com/en-us/library/aa730869%28VS.80%29.aspx
It worked for me.
最简单的方法是使用用户或应用程序设置
将 app.config 文件添加到项目中并编辑设置,如下所示
http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx#settingscs_topic2
The easiest way is to use user or application settings
Add an app.config file to your project and edit the settings like this
http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx#settingscs_topic2
过去,我创建了一个小的“设置”类。
然后我重写了 ToString 方法并创建了一个解析方法。
我已使用 global::System.Collections.Specialized.StringCollection 类型将每个设置存储在设置文件中,以存储设置集合。我使用 ToString 方法来存储设置,并使用 Parse 方法将其读回。
最后,我使用 int 类型来存储当前选定的索引。
In the past, I have created a small "settings" class.
I have then over ridden the ToString method and also created a parse method.
I have stored each setting in the settings file using a global::System.Collections.Specialized.StringCollection type to store a collection of settings. I use the ToString method to store the setting and the Parse method to read it back in.
Lastly, I use an int type to store the currently selected index.