Skip to content

Commit

Permalink
[TASK] add a few failsafes to ensure the files we need actually exist
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardberger committed Jun 30, 2023
1 parent 9c9f1f1 commit 79f6b8d
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions OpticalAdjustmentGUI/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ private void Form1_Load(object sender, EventArgs e)
{
this.disableControls();

if (!File.Exists("OpticalAdjustment.exe"))
{
var res = MessageBox.Show($"File not found: OpticalAdjustment.exe\n\nPlease move this tool to the same folder your OpticalAdjustment.exe is in!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}

if (this.checkBox1.Checked)
{
this.btnApply.Enabled = false;
Expand All @@ -32,16 +38,15 @@ private void Form1_Load(object sender, EventArgs e)
this.btnSave.Enabled = false;
}

if (File.Exists(this.filePath))
if (!File.Exists(this.filePath))
{
string content = File.ReadAllText(filePath);
this.value = Decimal.Parse(content);
numericUpDown1.Text = content;
File.CreateText(filePath).Dispose();
File.WriteAllText(filePath, "-0.4");
}
else
{

}
string content = File.ReadAllText(filePath);
this.value = Decimal.Parse(content);
numericUpDown1.Text = content;
}

private void btnApply_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -87,7 +92,20 @@ protected void deactivate()

protected void save(Decimal value)
{
File.WriteAllText(this.filePath, value.ToString());
try
{
File.WriteAllText(filePath, value.ToString());
}
catch (FileNotFoundException ex)
{
// Handle the exception when the file is not found
MessageBox.Show($"File not found: {ex.FileName}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
// Handle other exceptions
MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void disableControls()
Expand Down

0 comments on commit 79f6b8d

Please sign in to comment.