在winforms .Net 3.5 SP1中保存类库用户设置

发布于 2024-09-08 20:19:41 字数 1454 浏览 1 评论 0原文

不久前,我编写了自己的设置提供程序,因为在类库中保留设置时遇到问题。 (注意 - 我在保留应用程序中的设置方面没有任何问题)。

今天,我刚刚对持久类库设置进行了测试,并且在没有自定义提供程序的情况下它起作用了。 测试的是:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云归处 2024-09-15 20:19:41

我建议研究 .NET 的 XML 序列化内容。它工作得非常好,我几乎用它来存储我所有的设置。

基本上,创建一个将存储您的设置的类,确保它有一个不带参数的构造函数。

public class Settings
{
    public Settings() { }

    public string MySetting { get; set; }
}

然后在您的应用程序中,使用这样的函数将其序列化为 XML 文件:

public static string Serialize<T>(T item) where T:class
{
    try
    {
        XmlSerializer serial = new XmlSerializer(typeof(T));
        StringBuilder sb = new StringBuilder();
        StringWriter writer = new StringWriter(sb);
        serial.Serialize(writer, item);
        writer.Close();
        return sb.ToString();
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Failed to serialize object of type " + typeof(T).FullName + ": " + ex.Message);
        return "Failed to serialize";
    }
}

使用这样的函数将 XML 文件反序列化到您的设置类中:

public static T Deserialize<T>(string FilePath) where T : class
{
    try
    {
        XmlSerializer xml = new XmlSerializer(typeof(T));
        FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
        XmlSerializer xml = new XmlSerializer(typeof(T));
                T res = (T)xml.Deserialize(fs);
        fs.Close();
        return res;
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Failed to deserialize object of type " + typeof(T).FullName + ": " + ex.Message);
        return default(T);
    }
}

祝您好运,希望它能帮助您

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.

public class Settings
{
    public Settings() { }

    public string MySetting { get; set; }
}

And then from within your application, use a function like this to serialize it into an XML file:

public static string Serialize<T>(T item) where T:class
{
    try
    {
        XmlSerializer serial = new XmlSerializer(typeof(T));
        StringBuilder sb = new StringBuilder();
        StringWriter writer = new StringWriter(sb);
        serial.Serialize(writer, item);
        writer.Close();
        return sb.ToString();
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Failed to serialize object of type " + typeof(T).FullName + ": " + ex.Message);
        return "Failed to serialize";
    }
}

and a function like this to deserialize an XML file into your settings class:

public static T Deserialize<T>(string FilePath) where T : class
{
    try
    {
        XmlSerializer xml = new XmlSerializer(typeof(T));
        FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
        XmlSerializer xml = new XmlSerializer(typeof(T));
                T res = (T)xml.Deserialize(fs);
        fs.Close();
        return res;
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Failed to deserialize object of type " + typeof(T).FullName + ": " + ex.Message);
        return default(T);
    }
}

Good luck, and hope it helps you out

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文