Обновление
* Добавлено отображение пути открытого файла в заголовке окна * Исправлено появление сообщения что файл изменен при отсутствии изменений Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
@@ -8,49 +8,70 @@ namespace ConfigEditor
|
|||||||
{
|
{
|
||||||
public partial class MainForm: Form
|
public partial class MainForm: Form
|
||||||
{
|
{
|
||||||
private string opened_file = null;
|
private string opened_file_path = null;
|
||||||
|
private string[] original_file_data = null;
|
||||||
|
|
||||||
public MainForm()
|
public MainForm()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ClearData()
|
||||||
|
{
|
||||||
|
opened_file_path = null;
|
||||||
|
if (original_file_data != null)
|
||||||
|
Array.Clear(original_file_data, 0, original_file_data.Length);
|
||||||
|
richTextBox1.Clear();
|
||||||
|
ActiveForm.Text = "SaveWizard Config Editor";
|
||||||
|
}
|
||||||
|
|
||||||
private void openToolStripMenuItem_Click(object sender, EventArgs e)
|
private void openToolStripMenuItem_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
if (!IsModificationSaved())
|
||||||
|
return;
|
||||||
|
ClearData();
|
||||||
OpenFileDialog ofd = new OpenFileDialog()
|
OpenFileDialog ofd = new OpenFileDialog()
|
||||||
{
|
{
|
||||||
Title = "Выберите файл конфигурации",
|
Title = "Choose configuration file",
|
||||||
Filter = "Файл *.json|*.json",
|
Filter = "File *.json|*.json",
|
||||||
Multiselect = false
|
Multiselect = false
|
||||||
};
|
};
|
||||||
if (ofd.ShowDialog() == DialogResult.OK)
|
if (ofd.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
opened_file = ofd.FileName;
|
opened_file_path = ofd.FileName;
|
||||||
|
original_file_data = File.ReadAllLines(ofd.FileName);
|
||||||
richTextBox1.Text = File.ReadAllText(ofd.FileName);
|
richTextBox1.Text = File.ReadAllText(ofd.FileName);
|
||||||
|
ActiveForm.Text = $"{ofd.FileName} - SaveWizard Config Editor";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
|
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
File.WriteAllText(opened_file, richTextBox1.Text);
|
if (opened_file_path == null)
|
||||||
|
return;
|
||||||
|
File.WriteAllText(opened_file_path, richTextBox1.Text);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
|
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
SaveFileDialog sfd = new SaveFileDialog
|
SaveFileDialog sfd = new SaveFileDialog
|
||||||
{
|
{
|
||||||
Title = "Сохранить файл конфигурации",
|
Title = "Save configuration file",
|
||||||
Filter = "Файл *.json|*.json"
|
Filter = "File *.json|*.json"
|
||||||
};
|
};
|
||||||
if (sfd.ShowDialog() == DialogResult.OK)
|
if (sfd.ShowDialog() == DialogResult.OK)
|
||||||
|
{
|
||||||
File.WriteAllText(sfd.FileName, richTextBox1.Text);
|
File.WriteAllText(sfd.FileName, richTextBox1.Text);
|
||||||
|
opened_file_path = sfd.FileName;
|
||||||
|
ActiveForm.Text = $"{sfd.FileName} - SaveWizard Config Editor";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateMD5ToolStripMenuItem_Click(object sender, EventArgs e)
|
private void generateMD5ToolStripMenuItem_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
using (var md5 = MD5.Create())
|
using (var md5 = MD5.Create())
|
||||||
{
|
{
|
||||||
using (FileStream stream = File.OpenRead(opened_file))
|
using (FileStream stream = File.OpenRead(opened_file_path))
|
||||||
md5.ComputeHash(stream);
|
md5.ComputeHash(stream);
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
@@ -63,44 +84,57 @@ namespace ConfigEditor
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool CheckChanges()
|
private bool IsModificationSaved()
|
||||||
{
|
{
|
||||||
if (opened_file != null)
|
if (opened_file_path == null)
|
||||||
|
return true;
|
||||||
|
bool isDataEquals = true;
|
||||||
|
string[] rtbLines = richTextBox1.Text.Split(
|
||||||
|
new[] { "\r\n", "\r", "\n" },
|
||||||
|
StringSplitOptions.None
|
||||||
|
);
|
||||||
|
for (int i = 0; i < original_file_data.Length; i++)
|
||||||
{
|
{
|
||||||
if (richTextBox1.Text != File.ReadAllText(opened_file))
|
if (rtbLines[i] != original_file_data[i])
|
||||||
{
|
{
|
||||||
|
isDataEquals = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isDataEquals)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
DialogResult result = MessageBox.Show("The document has been modified.\nDo you want to save your changes?", "Question", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
|
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)
|
if (result == DialogResult.Yes)
|
||||||
{
|
{
|
||||||
File.WriteAllText(opened_file, richTextBox1.Text);
|
File.WriteAllText(opened_file_path, richTextBox1.Text);
|
||||||
MessageBox.Show("File successfully saved.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
MessageBox.Show("File successfully saved.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
return true;
|
return true;
|
||||||
}
|
} else if (result == DialogResult.No)
|
||||||
|
return true;
|
||||||
else if (result == DialogResult.Cancel)
|
else if (result == DialogResult.Cancel)
|
||||||
return false;
|
return false;
|
||||||
}
|
return false;
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void closeFileToolStripMenuItem_Click(object sender, EventArgs e)
|
private void closeFileToolStripMenuItem_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if (!CheckChanges())
|
if (!IsModificationSaved())
|
||||||
return;
|
return;
|
||||||
richTextBox1.Clear();
|
ClearData();
|
||||||
opened_file = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
|
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if (!CheckChanges())
|
if (!IsModificationSaved())
|
||||||
return;
|
return;
|
||||||
Application.Exit();
|
Application.Exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
|
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
|
||||||
{
|
{
|
||||||
if (!CheckChanges())
|
if (!IsModificationSaved())
|
||||||
return;
|
return;
|
||||||
Application.Exit();
|
Application.Exit();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user