|
@@ -1,6 +1,5 @@
|
|
|
using System;
|
|
|
using System.IO;
|
|
|
-using System.Runtime.Remoting.Messaging;
|
|
|
using System.Windows.Forms;
|
|
|
using File = System.IO.File;
|
|
|
|
|
@@ -12,13 +11,15 @@ namespace FileExpander
|
|
|
private const int Megabyte = Kilobyte * 1024;
|
|
|
private const int Gigabyte = Megabyte * 1024;
|
|
|
|
|
|
+ private string _selectedFile;
|
|
|
+
|
|
|
public FileExpanderForm() => InitializeComponent();
|
|
|
|
|
|
private void SelectButton_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
using var dlg = new OpenFileDialog { Title = "Select file to expand", CheckFileExists = true };
|
|
|
if (dlg.ShowDialog() != DialogResult.OK) return;
|
|
|
- FileValueLabel.Text = dlg.FileName;
|
|
|
+ _selectedFile = FileValueLabel.Text = dlg.FileName;
|
|
|
UpdateDisplay();
|
|
|
}
|
|
|
|
|
@@ -32,7 +33,12 @@ namespace FileExpander
|
|
|
|
|
|
private void FileValueLabel_DragDrop(object sender, DragEventArgs e)
|
|
|
{
|
|
|
- FileValueLabel.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
|
|
|
+ _selectedFile = FileValueLabel.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
|
|
|
+ UpdateDisplay();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ExpandNumericUpDown_ValueChanged(object sender, EventArgs e)
|
|
|
+ {
|
|
|
UpdateDisplay();
|
|
|
}
|
|
|
|
|
@@ -45,7 +51,19 @@ namespace FileExpander
|
|
|
|
|
|
private void GoButton_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
- using var fs = new FileStream(FileValueLabel.Text, FileMode.Open, FileAccess.Write, FileShare.None);
|
|
|
+ if (null == _selectedFile)
|
|
|
+ {
|
|
|
+ MessageBox.Show("No file selected!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!File.Exists(_selectedFile))
|
|
|
+ {
|
|
|
+ MessageBox.Show("No such file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ using var fs = new FileStream(_selectedFile, FileMode.Open, FileAccess.Write, FileShare.None);
|
|
|
fs.SetLength(fs.Length + GetExpand());
|
|
|
fs.Close();
|
|
|
MessageBox.Show("Success");
|
|
@@ -56,8 +74,14 @@ namespace FileExpander
|
|
|
|
|
|
private void UpdateDisplay()
|
|
|
{
|
|
|
- var file = FileValueLabel.Text;
|
|
|
- if (!File.Exists(file))
|
|
|
+ if (null == _selectedFile)
|
|
|
+ {
|
|
|
+ SizeValueLabel.Text = "⚠ No file selected";
|
|
|
+ AfterExpandValueLabel.Text = "";
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!File.Exists(_selectedFile))
|
|
|
{
|
|
|
SizeValueLabel.Text = "⚠ No such file";
|
|
|
AfterExpandValueLabel.Text = "";
|
|
@@ -68,7 +92,7 @@ namespace FileExpander
|
|
|
try
|
|
|
{
|
|
|
//get real file size
|
|
|
- using var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
|
+ using var fs = new FileStream(_selectedFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
|
size = fs.Length;
|
|
|
fs.Close();
|
|
|
}
|
|
@@ -121,7 +145,5 @@ namespace FileExpander
|
|
|
var result = String.Format("{0:0.##} {1}", len, sizes[order]);
|
|
|
return result;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
}
|