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); // , . } } }
using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Text;
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; } } }
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); // , . } } }
Source: https://habr.com/ru/post/271483/
All Articles