Created
December 20, 2022 16:33
-
-
Save proffix4/168b17e6d020ddfcb8af87ac146aefb3 to your computer and use it in GitHub Desktop.
Работа с ini-файлами на C# для Windows в Visual Studio 2022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace TSN_CSh_lab6 | |
{ | |
public partial class Form1 : Form | |
{ | |
public const string FILE_NAME = "Settings.ini"; // Имя файла настроек | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
// Запись данных из компонент в ini-файл при закрытии формы | |
private void Form1_FormClosing(object sender, FormClosingEventArgs e) | |
{ | |
if (checkBox2.Checked) | |
{ | |
new IniFile(FILE_NAME).DeleteFile(); | |
} | |
else | |
{ | |
IniFile MyIni = new(FILE_NAME); | |
MyIni.WriteKey("Text", textBox1.Text, "textBox1"); | |
MyIni.WriteKey("Checked", checkBox1.Checked.ToString(), "checkBox1"); | |
MyIni.WriteKey("Length", richTextBox1.Lines.Length.ToString(), "richTextBox1"); | |
for (int i = 0; i < richTextBox1.Lines.Length; i++) | |
{ | |
MyIni.WriteKey("Lines_" + i.ToString(), richTextBox1.Lines[i], "richTextBox1"); | |
} | |
} | |
} | |
// Чтение и восстановление данных из ini-файла при загрузке формы | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
var MyIni = new IniFile(FILE_NAME); | |
if (MyIni.KeyExists("Text", "textBox1")) | |
{ | |
textBox1.Text = MyIni.ReadKey("Text", "textBox1"); | |
} | |
if (MyIni.KeyExists("Checked", "checkBox1")) | |
{ | |
try | |
{ | |
checkBox1.Checked = Convert.ToBoolean(MyIni.ReadKey("Checked", "checkBox1")); | |
} | |
catch (Exception) { } | |
} | |
if (MyIni.KeyExists("Length", "richTextBox1")) | |
{ | |
try | |
{ | |
int count = Convert.ToInt32(MyIni.ReadKey("Length", "richTextBox1")); | |
richTextBox1.Clear(); | |
for (int i = 0; i < count; i++) | |
{ | |
richTextBox1.AppendText(MyIni.ReadKey("Lines_" + i.ToString(), "richTextBox1")); | |
if (i < count - 1) | |
{ | |
richTextBox1.AppendText(Environment.NewLine); | |
} | |
} | |
} | |
catch (Exception ex) { } | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment