将所有表单属性保存在文件中的方法 [英] way to save all form properties in a file

查看:75
本文介绍了将所有表单属性保存在文件中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以有效的方式保存所有表单信息,如窗口状态,宽度,顶部,高度,左边和字体大小,字体类型和关闭表单的颜色在一个文件中我可以自由选择哪个目录以及哪个名称我可以自由选择。是否有任何.NET功能。我不是指应用程序设置,因为这并没有满足我控制保存文件的位置的要求,而且我不需要那些数据类型。我也无法在File命名空间中找到INI访问函数。还有其他想法吗?



我想选择保存这些数据的目录和文件名。使用应用程序设置,我看不到这样做。我甚至不知道数据的存储位置,因此无法备份。所以我寻找另一种常见且专业的方式来做到这一点。



更新:

我绝对希望指定数据的位置,绝对希望在NET 2.0中使用.NET内置函数将其保存为二进制或INI存储格式。

我绝对希望首先保存表单数据。

how would I save all form information in a valid way like window state, width, top, height, left and font size, font type and color of the closing form in a file which directory I can choose freely and which name I can choose freely. Is there any .NET function for it. I don't mean Application Settings since this did not fullfill my requirements of controlling where to save the file and there are not those data types which I would need. I also could not find INI access functions in the File namespace. Any other ideas?

I would like to choose the directory and filename where this data is saved to. With Application Settings I see no way of doing this. I even don't know where the data is stored and therefore can't backup it. So I search for another common and professional way of doing this easier.

Update:
I definitely wish to specify the location of the data and definitely wish to save it in a binary or INI storage format with .NET inbuilt functions within NET 2.0.
I definitely wish only to save the forms data at first.

推荐答案

解决方案2为您提供了一个好主意。您可以使用它或使用一些固定的数据结构;我想在保存和恢复一些任意数据的过程中添加解决方案:



我认为应用程序设置是过时的功能,它对我来说不够灵活,所以我从不使用它,也从不提出建议。有一种更新,更强大的技术,它是序列化的形式之一:

https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx [ ^ 到目前为止,这种方法最容易使用且非常强大:您根本不必考虑文件操作(XML,in这个案例)。您只需从包含一种或多种数据类型的任何数据结构中签订合同。该方法是非侵入式的,对数据类没有基本要求或限制;您只需添加一些不会修改除持久性之外的任何行为的属性。序列化程序将任意对象图(甚至不必是树)保存到任何流中,以便以后的方式将其恢复到内存中。



请参阅我过去的答案,提倡这种方法:

如何在表单应用程序中使用XML文件编写器和阅读器? [ ^ ],

创建属性文件...... [< a href =http://www.codeproject.com/Answers/176011/Creating-a-property-files#answer3target =_ blanktitle =New Window> ^ ]。



-SA
Solution 2 gives you a good idea. You can use it or use some fixed data structures; I want to add the solution in part of saving and restoring some arbitrary data:

I think Application Settings is the obsolete feature, it is not flexible enough to me, so I never use it and never advise. There is a newer, much more robust technique, which is one of the forms of serialization:
https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

This approach is, by far, the easiest to use and very robust: you don't have to think about the file operations at all (XML, in this case). You just make the contract from any data structures consisting of one or several data types. The approach is non-intrusive, there are no essential requirements or limitations to your data classes; you just add some attributes which don't modify any behavior except the persistence. The serializer saves any arbitrary object graph (does not even have to be a tree) to any stream, to later restore it in memory the way it was before.

Please see my past answer advocating this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA


参见使用应用程序设置的用户首选项 [ ^ ]。


我的立场得到了纠正。我实际上考虑的是getType()。getProperties()而不是属性。自从我使用它们已经有一段时间了。



我建议创建一个辅助类,它将使用反射迭代实例属性。



以下是一些参考链接:

这将为您提供一系列PropertyInfo对象。

https://msdn.microsoft.com/en-us/library/aky14axb(ⅴ = vs.110).aspx [ ^ ]



使用PropertyInfo实例,您可以调用GetValue来检索每个属性值。

https://msdn.microsoft.com/en-us /library/system.reflection.propertyinfo(v=vs.110).aspx [ ^ ]



这个助手类可以定制为具有输出文件名和位置,用于保存找到的属性和使用StreamWriter你可以保存这些属性。
I stand corrected. Instead of attributes I actually was thinking of getType().getProperties(). It's been some time since I used them.

I suggest creating a helper class that will iterate through a instance properties using reflection.

Here are some reference links :
This will get you an array of PropertyInfo objects.
https://msdn.microsoft.com/en-us/library/aky14axb(v=vs.110).aspx[^]

Using the PropertyInfo instance you can call GetValue to retrieve each properties value.
https://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo(v=vs.110).aspx[^]

This helper class can be customized to have a output file name and location where to save the found attributes and using a StreamWriter you can save these attributes.


这篇关于将所有表单属性保存在文件中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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