123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using System;
- using System.IO;
- using System.Windows.Forms;
- using File = System.IO.File;
- namespace FileExpander
- {
- public partial class FileExpanderForm : Form
- {
- private const int Kilobyte = 1024;
- 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;
- _selectedFile = FileValueLabel.Text = dlg.FileName;
- UpdateDisplay();
- }
- private void FileValueLabel_DragEnter(object sender, DragEventArgs e)
- {
- e.Effect =
- e.Data.GetDataPresent(DataFormats.FileDrop) && e.Data.GetData(DataFormats.FileDrop) is string[] arr && arr.Length == 1 && File.Exists(arr[0])
- ? DragDropEffects.Link
- : DragDropEffects.None;
- }
- private void FileValueLabel_DragDrop(object sender, DragEventArgs e)
- {
- _selectedFile = FileValueLabel.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
- UpdateDisplay();
- }
- private void ExpandNumericUpDown_ValueChanged(object sender, EventArgs e)
- {
- UpdateDisplay();
- }
- private void UnitRadioButton_CheckedChanged(object sender, EventArgs e)
- {
- var self = (RadioButton)sender;
- if (self.Checked == false) return;
- UpdateDisplay();
- }
- private void GoButton_Click(object sender, EventArgs e)
- {
- 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");
- Close();
- }
- //---------------------------
- private void UpdateDisplay()
- {
- if (null == _selectedFile)
- {
- SizeValueLabel.Text = "⚠ No file selected";
- AfterExpandValueLabel.Text = "";
- return;
- }
- if (!File.Exists(_selectedFile))
- {
- SizeValueLabel.Text = "⚠ No such file";
- AfterExpandValueLabel.Text = "";
- return;
- }
- long size;
- try
- {
- //get real file size
- using var fs = new FileStream(_selectedFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- size = fs.Length;
- fs.Close();
- }
- catch (Exception e)
- {
- SizeValueLabel.Text = "⚠ Failure to get file size:" + e.Message;
- AfterExpandValueLabel.Text = "";
- return;
- }
- SizeValueLabel.Text = HumanReadableSize(size) + $" ( {size:N0} bytes )";
- var expand = GetExpand();
- if (expand == -1)
- {
- AfterExpandValueLabel.Text = "- No Unit Selected -";
- }
- else
- {
- var final = size + expand;
- AfterExpandValueLabel.Text = HumanReadableSize(final) + $" ( {final:N0} bytes )";
- }
- }
- private long GetExpand()
- {
- long expand = -1;
- if (KbRadioButton.Checked) expand = (long)ExpandNumericUpDown.Value * Kilobyte;
- else if (MbRadioButton.Checked) expand = (long)ExpandNumericUpDown.Value * Megabyte;
- else if (GbRadioButton.Checked) expand = (long)ExpandNumericUpDown.Value * Gigabyte;
- return expand;
- }
- private static string HumanReadableSize(long size)
- {
- var sizes = new[] { "B", "KB", "MB", "GB", "TB" };
- double len = size;
- var order = 0;
- while (len >= 1024 && order < sizes.Length - 1)
- {
- order++;
- len /= 1024;
- }
- // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
- // show a single decimal place, and no space.
- var result = String.Format("{0:0.##} {1}", len, sizes[order]);
- return result;
- }
- }
- }
|