Реализован базовый функционал редактора конфигов

Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2025-04-02 19:37:51 +07:00
parent 6ad64eb92c
commit c94f9554a8
2 changed files with 147 additions and 59 deletions

View File

@@ -1,16 +1,21 @@
using System.IO;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace ConfigEditor
{
public partial class MainForm: Form
{
private string opened_file = null;
public MainForm()
{
InitializeComponent();
}
private void открытьToolStripMenuItem_Click(object sender, System.EventArgs e)
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog()
{
@@ -19,11 +24,81 @@ namespace ConfigEditor
Multiselect = false
};
if (ofd.ShowDialog() == DialogResult.OK)
{
opened_file = ofd.FileName;
richTextBox1.Text = File.ReadAllText(ofd.FileName);
}
}
private void закрытьToolStripMenuItem_Click(object sender, System.EventArgs e)
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
File.WriteAllText(opened_file, richTextBox1.Text);
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog
{
Title = "Сохранить файл конфигурации",
Filter = "Файл *.json|*.json"
};
if (sfd.ShowDialog() == DialogResult.OK)
File.WriteAllText(sfd.FileName, richTextBox1.Text);
}
private void generateMD5ToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var md5 = MD5.Create())
{
using (FileStream stream = File.OpenRead(opened_file))
md5.ComputeHash(stream);
StringBuilder sb = new StringBuilder();
byte[] hash = md5.Hash;
for (int i = 0; i < hash.Length; i++)
sb.Append(hash[i].ToString("x2"));
Clipboard.SetText(sb.ToString());
MessageBox.Show($"Hash successfully copied into your clipboard.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private bool CheckChanges()
{
if (richTextBox1.Text != File.ReadAllText(opened_file))
{
DialogResult result = MessageBox.Show("The document has been modified.\nDo you want to save your changes?", "Question", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
File.WriteAllText(opened_file, richTextBox1.Text);
MessageBox.Show("File successfully saved.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
else if (result == DialogResult.Cancel)
return false;
}
return true;
}
private void closeFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!CheckChanges())
return;
richTextBox1.Clear();
opened_file = null;
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!CheckChanges())
return;
Application.Exit();
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
if (!CheckChanges())
return;
Application.Exit();
}
}