7

I'm writing my first Windows Forms application using VS 2010 and C#. It does not use a database but I would like to save user settings like directory path and what check boxes are checked. What is the easiest way to save these preferences?

Erick Petrucelli
  • 14,386
  • 8
  • 64
  • 84
Paul
  • 757
  • 3
  • 10
  • 38
  • Possible duplicate of [What is the best way to store user settings for a .NET application?](https://stackoverflow.com/questions/26369/what-is-the-best-way-to-store-user-settings-for-a-net-application) – Franklin Yu Oct 06 '17 at 15:57

5 Answers5

9

I suggest you to use the builtin application Settings to do it. Here is an article talking about it.

Sample usage:

MyProject.Properties.Settings.Default.MyProperty = "Something";
Erick Petrucelli
  • 14,386
  • 8
  • 64
  • 84
7

You can use the serializable attribute in conjunction with a 'settings' class. For small amount of information this is really your best bet as it is simple to implement. For example:

  [Serializable]
  public class MySettings  
  {
    public const string Extension = ".testInfo";

    [XmlElement]
    public string GUID { get; set; }

    [XmlElement]
    public bool TurnedOn { get; set; }

    [XmlElement]
    public DateTime StartTime { get; set; }

    public void Save(string filePath)
    {
      XmlSerializer serializer = new XmlSerializer(typeof(MySettings));
      TextWriter textWriter = new StreamWriter(filePath);
      serializer.Serialize(textWriter, this);
      textWriter.Close();
    }

    public static MySettings Load(string filePath)
    {
      XmlSerializer serializer = new XmlSerializer(typeof(MySettings));
      TextReader reader = new StreamReader(filePath);
      MySettings data = (MySettings)serializer.Deserialize(reader);
      reader.Close();

      return data;
    }
  }

There you go. You can prety much cut and paste this directly into your code. Just add properties as needed, and don't forget the [XMLElement] attribute on your interesting properties.

Another benefit to this design is that you don't have to fiddle with the cumbersome Application.Settings approaches, and you can modify your files by hand if you need to.

A.R.
  • 15,405
  • 19
  • 77
  • 123
  • This is a really good answer. It allows you much freedom while keeping you code elegant. What is missing is the logic for checking if returned properties are null and valid. This can happen if you get a corrupted settings .xml file. – Vladimir Mar 20 '15 at 14:45
  • @Vladimir Thanks for the compliment. I suppose that one could add the checking logic + add reasonable defaults in the Load() function. Corrupted files are a tricky business, so the amount of code to do so could be quite high. – A.R. Mar 20 '15 at 15:59
2

I'd save the settings in an XML file. That way it's easy for the user to share their settings across machines etc.

You'll also be able to deserialize the XML as a class in your application, giving you easy access to the settings you require.

Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
2

The easiest way would be in the app.config settings which you can set in the designer under project properties settings (make sure you set them as user setting not application settings or you wont be able to save them) you can then read and write them with C#

to read write just access properties on

Properties.Settings.Default.<your property>

there are also methods to save the properties to the users profile or to reset to defaults

Properties.Settings.Default.Reset();
Properties.Settings.Default.Save();

http://msdn.microsoft.com/en-us/library/a65txexh.aspx

ridoy
  • 6,274
  • 2
  • 29
  • 60
jk.
  • 13,817
  • 5
  • 37
  • 50
0

What about adding a local dataset to your project (then create a setting table) and export the data finally to an xml file, its easy to use and more fixable

enter image description here

1- add columns (e.g.; settingName and settingValue) to your local table (DataTable1) using the designer, then

//data set
 DataSet1 ds = new DataSet1();
 DataSet1.DataTable1DataTable settingsTable = (DataSet1.DataTable1DataTable)ds.Tables[0];
 //add new setting 
 settingsTable.Rows.Add(new string[] { "setting1", "settingvalue1" });
 //export to xml file
 settingsTable.WriteXml("settings.xml");

 //if you want to read ur settings back... read from xml
 settingsTable.ReadXml("settings.xml");
Rami Alshareef
  • 7,015
  • 12
  • 47
  • 75