在winforms .Net 3.5 SP1中保存类库用户设置
不久前,我编写了自己的设置提供程序,因为在类库中保留设置时遇到问题。 (注意 - 我在保留应用程序中的设置方面没有任何问题)。
今天,我刚刚对持久类库设置进行了测试,并且在没有自定义提供程序的情况下它起作用了。 测试的是:
(1)创建类库
(2) 添加设置 - '名称'
(3) 添加一个具有公共共享属性“Name”的类,该类可读取和写入设置“Name”。
(4) 创建一个引用类库的应用程序,并使用共享属性来读写“Name”设置。
我确信在应用程序打开时“名称”设置将持续存在,但是在我关闭它并重新打开它之后,该设置将恢复为默认值。 现在这种情况不会发生,我想确定我以前是个白痴,而不是现在的白痴。
我现在得到的行为是预期的行为吗?
ETA:我注意到,第一次创建测试应用程序、更改设置、关闭并重新打开时,该设置不会保留。随后便是。可能是我之前尝试过一次之后就放弃了。有什么想法为什么第一次没有坚持下来吗?
ETA2:作为示例,我有一个名为“MyLibrary”的类库和一个名为“MyApp”的测试应用程序。我执行了上面的步骤 1-4,下面是位于 C:\Documents and Settings\User Name\Local Settings\Application Data\MyApp\MyApp.vshost.exe_Url_vi5gjcooahbdm2ma3dcay0mkexu2suul\1.0.0.0 的 user.config 文件的内容。注意:我没有触及 MyApp 中的设置;-
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral >
<section name="MyLibrary.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<MyLibrary.My.MySettings>
<setting name="Name" serializeAs="String">
<value>New changed value</value>
</setting>
</MyLibrary.My.MySettings>
</userSettings>
A while back, I wrote my own settings provider because I was having problems persisting settings in a class library. (Note - I had no problems persisting settings from an application).
Today, I just did a test of persisting class library settings and it worked, without my custom provider.
The test is:
(1) Create a class library
(2) Add a setting - 'Name'
(3) Add a class with a public shared property 'Name' that reads and writes to the setting 'Name'.
(4) Create an application that references the class library, and use the shared property to read and write the 'Name' setting.
I'm sure that before the 'Name' setting would persist while the application was open but after I closed it and re-opened it, the setting would revert to the default.
This is not happening now and I'd like to be sure that I was being an idiot before and not an idiot now.
Is the behaviour I'm getting now the expected behaviour?
ETA: I've noticed that the first time I create a test app, change the setting, close, and reopen, the setting is not persisted. Subsequently it is. May be I gave up after one try before. Any ideas why it's not persisted the first time?
ETA2: As an example, I have a class library called 'MyLibrary' and a test application called 'MyApp'. I do the steps 1-4 above, and below are the contents of the user.config file found at C:\Documents and Settings\User Name\Local Settings\Application Data\MyApp\MyApp.vshost.exe_Url_vi5gjcooahbdm2ma3dcay0mkexu2suul\1.0.0.0. Note: I did not touch the settings in MyApp;-
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral >
<section name="MyLibrary.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<MyLibrary.My.MySettings>
<setting name="Name" serializeAs="String">
<value>New changed value</value>
</setting>
</MyLibrary.My.MySettings>
</userSettings>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议研究 .NET 的 XML 序列化内容。它工作得非常好,我几乎用它来存储我所有的设置。
基本上,创建一个将存储您的设置的类,确保它有一个不带参数的构造函数。
然后在您的应用程序中,使用这样的函数将其序列化为 XML 文件:
使用这样的函数将 XML 文件反序列化到您的设置类中:
祝您好运,希望它能帮助您
I'd recommend looking into .NET's XML Serialization stuff. It works really well, and I use it for almost all my settings storage stuff.
Basically, create a class that will store your settings, making sure it has a constructor that takes no arguments.
And then from within your application, use a function like this to serialize it into an XML file:
and a function like this to deserialize an XML file into your settings class:
Good luck, and hope it helps you out