Saving simple user preferences in Windows Forms using C #

I am 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, such as the directory path, and which checkboxes are checked. What is the easiest way to save these settings?

+6
source share
5 answers

I suggest you use the built-in Settings app for this. Here is an article about it.

Sample Usage:

 MyProject.Properties.Settings.Default.MyProperty = "Something"; 
+9
source

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

  [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 cut and paste it a lot directly into your code. Just add properties as needed and don't forget the [XMLElement] attribute in your interesting properties.

Another advantage of this design is that you don’t have to bother with the cumbersome Application.Settings approaches, and you can change your files manually if you need to.

+5
source

I would save the settings in an XML file. Thus, the user can easily share their settings between machines, etc.

You can also deserialize XML as a class in your application, giving you easy access to the required parameters.

+2
source

The easiest way can be found in the app.config settings, which you can set in the designer in the project properties settings (make sure you set them as user settings, not application settings, or you can’t save them), you can read and write them using c #

to read only property permissions on

 Properties.Settings.Default.<your property> 

There are also ways to save properties in a user profile or reset to default

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

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

+2
source

How to add a local data set to your project (then create a table of parameters) and export the data to an xml file, its easy to use and more fixed

enter image description here

1- add columns (e.g. settingName and settingValue) to your local table (DataTable1) using the constructor, 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"); 
0
source

Source: https://habr.com/ru/post/889950/


All Articles