开发者

Saving class library user settings in winforms .Net 3.5 SP1

开发者 https://www.devze.com 2023-01-06 00:17 出处:网络
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).

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开发者_StackOverflow社区\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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消