简体   繁体   中英

C# Dynamically make settings within application then save persistently

I am finding a lot of different ways to do this and I'm not sure the direction I should go...

I have an application that will run on several personal computers. I am looking for a way to keep a list of application settings persistently.

The idea being that the user will be able to choose amongst a list of applications. Those applications will then be saved until the user removes them. I need to save the application name and the corresponding path.

The problem is that I can't seem to save the key, value pairs to new settings in visual studio and have them persist. I need to write a file to save the files, how do I go about doing that... Should I write them to system.configuration, JSON or XML??? Does anyone have a good walkthrough?

Well, there are a lot of ways to do that. For a simple approach, you can use XML serialization. First create a class that represents all the settings you want to save, and add the Serializable attribute to it, for example:

[Serializable]
public class AppSettings
{
    public List<UserApp> Applications { get; set; }
}

[Serializable]
public class UserApp
{
    public string Path { get; set; }
    public string Name { get; set; }
}

Then, add the following methods to it:

public static void Save(AppSettings settings)
{
    string xmlText = string.Empty;
    var xs = new XmlSerializer(settings.GetType());
    using (var xml = new StringWriter())
    {
        xs.Serialize(xml, settings);
        xml.Flush();
        xmlText = xml.ToString();
    }
    string roamingPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    File.WriteAllText(roamingPath + @"\settings.xml", xmlText);
}

public static AppSettings Load()
{
    string roamingPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

    if (!File.Exists(roamingPath + @"\settings.xml"))
        return new AppSettings();

    string xmlText = File.ReadAllText(roamingPath + @"\settings.xml");
    var xs = new XmlSerializer(typeof(AppSettings));
    return (AppSettings)xs.Deserialize(new StringReader(xmlText));
}

Then, to save, do:

AppSettings settings = new AppSettings();
settings.Applications = new List<UserApp>();

settings.Applications.Add(new UserApp { Path = @"C:\bla\foo.exe", Name = "foo" });

AppSettings.Save(settings);

And to load:

AppSettings settings = AppSettings.Load();

You can also edit the loaded settings and save it again, overwriting the older.

For more a more complex approach, save into a database.

Add a setting to the settings using the instructions shown in below screenshot:

NOTE: Double click Properties shown with first arrow.

在此处输入图片说明

Then you can update that value at runtime like this:

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main()
        {
            var defSettings = ConsoleApplication1.Properties.Settings.Default;
            var props = defSettings.Test = "Whatever";

            // Save it so it persists between application start-ups
            defSettings.Save();

            Console.Read();
        }
    }
}

The settings will be stored in the user's profile .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM