📜 ⬆️ ⬇️

C #, ways to store program settings

Introduction


There are many ways to store the program settings on the Internet, but they are somehow scattered, so I decided to put them together and describe how to use it.

C # and app.config


On Habré has already been devoted to this topic, so ... go

C # and Properties.Settings


Information on Properties.Settings
')
The Properties.Settings organization is a regular xml file that can be found in the user’s folder:

C: \ Users \ [user name] \ AppData \ Local \ [(Project Name) or (AssemblyCompany)] \ [name project_cashBuild] \ [AssemblyVersion] \ user.config

First of all, we need to create such variables for Properties.Settings. Go to Properties -> Settings.settings:

Properties -> Settings.settings in VS 2013


I created 3rd variables and chose the area of ​​their use: 2- user area and 1-application.

The distinction between areas is simple. The application area can only be read, and the user can change and read.

Let's return to variables:

  • Version - the version of our program. The application defined it with a string and a region. Since the version may contain letters (for example, b - from beta). And the region has chosen, so that our version of the application does not change (since AssemblyVersion is rarely used).
  • Save_text is a variable where we will save our text.
  • open_sum - how many times we opened the program.

Now let's go to the code

Form1.cs Code
namespace Habrahabr { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Text += " " + Properties.Settings.Default.Version; //   , . Properties.Settings.Default.open_sum++; // +1  -  . label2.Text = Properties.Settings.Default.open_sum.ToString(); //  Label2 -  . richTextBox1.Text = Properties.Settings.Default.Save_text; //     Properties.Settings.Default.Save(); //  . } private void button1_Click(object sender, EventArgs e) { Properties.Settings.Default.Save_text = richTextBox1.Text; //   richTextBox1  Save_text Properties.Settings.Default.Save(); //  . MessageBox.Show(" ", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); //  ,   . } } } 


The results of the program

The first launch, we see that the number of launches is 1. And there is no test in richTextBox1.



Now we will write and save the text.



At the second launch, we see that the text is saved, and the number of launches is already 2-va.



Conclusion

It is very convenient to use this object if you need to work in different areas of visibility in one project. The method is good when you don’t need an ordinary user to search through the program’s configuration files.

C # and ini files


With ini-files all the way around, they are in the folder next to the program, which allows the user to change the settings of the off-program. This method is good if the program settings are entered manually. For example, an emulator for launching a game without a license (revLoader).

We now turn to our topic. To work with this type of file, we need to create a class for working with it. Create a class, for example, "IniFile", connect the namespace, which is not:

 using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Text; 

And now we analyze in order:

IniFiles.cs code
 using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Text; namespace IniFiles { class IniFile { string Path; // . [DllImport("kernel32")] //  kernel32.dll     WritePrivateProfilesString static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath); [DllImport("kernel32")] //    kernel32.dll,     GetPrivateProfileString static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath); //          . public IniFile(string IniPath) { Path = new FileInfo(IniPath).FullName.ToString(); } // ini-        . public string ReadINI(string Section, string Key) { var RetVal = new StringBuilder(255); GetPrivateProfileString(Section, Key, "", RetVal, 255, Path); return RetVal.ToString(); } //  ini-.        . public void Write(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, Path); } //    . public void DeleteKey(string Key, string Section = null) { Write(Section, Key, null); } //   public void DeleteSection(string Section = null) { Write(Section, null, null); } //,    ,    public bool KeyExists(string Key, string Section = null) { return ReadINI(Section, Key).Length > 0; } } } 


Now go to the main program.

Form1.cs Code
 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace IniFiles { public partial class Form1 : Form { IniFile INI = new IniFile("config.ini"); public Form1() { InitializeComponent(); auto_read(); } private void auto_read() { if (INI.KeyExistsINI("SettingForm1", "Width")) numericUpDown2.Value = int.Parse(INI.ReadINI("SettingForm1", "Height")); else numericUpDown1.Value = this.MinimumSize.Height; if (INI.KeyExistsINI("SettingForm1", "Height")) numericUpDown1.Value = int.Parse(INI.ReadINI("SettingForm1", "Width")); else numericUpDown2.Value = this.MinimumSize.Width; if (INI.KeyExistsINI("SettingForm1", "Width")) textBox1.Text = INI.ReadINI("Other", "Text"); this.Height = int.Parse(numericUpDown1.Value.ToString()); this.Width = int.Parse(numericUpDown2.Value.ToString()); } private void button1_Click(object sender, EventArgs e) { INI.WriteINI("SettingForm1", "Height", numericUpDown2.Value.ToString()); INI.WriteINI("SettingForm1", "Width", numericUpDown1.Value.ToString()); INI.WriteINI("Other", "Text", textBox1.Text); MessageBox.Show(" SettingForm1  Other ", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); //  ,   . } private void button2_Click(object sender, EventArgs e) { auto_read(); //   . } private void button3_Click(object sender, EventArgs e) { INI.WriteINI("SettingForm1", "Height", numericUpDown2.Value.ToString()); INI.WriteINI("SettingForm1", "Width", numericUpDown1.Value.ToString()); this.Height = int.Parse(numericUpDown1.Value.ToString()); this.Width = int.Parse(numericUpDown2.Value.ToString()); MessageBox.Show(" SettingForm1   ", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); //  ,   . } } } 


The results of the program

When you first start, we do not have a config.ini file. Therefore, when checking, the fasle returns and we equate the window with the minimum parameters.



Change the window settings and click "Apply"



Edit the config.ini file by hand and click download.



That's all, next time I will describe the work with xml files and binary files.

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


All Articles