FileExpanderForm.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using File = System.IO.File;
  5. namespace FileExpander
  6. {
  7. public partial class FileExpanderForm : Form
  8. {
  9. private const int Kilobyte = 1024;
  10. private const int Megabyte = Kilobyte * 1024;
  11. private const int Gigabyte = Megabyte * 1024;
  12. private string _selectedFile;
  13. public FileExpanderForm() => InitializeComponent();
  14. private void SelectButton_Click(object sender, EventArgs e)
  15. {
  16. using var dlg = new OpenFileDialog { Title = "Select file to expand", CheckFileExists = true };
  17. if (dlg.ShowDialog() != DialogResult.OK) return;
  18. _selectedFile = FileValueLabel.Text = dlg.FileName;
  19. UpdateDisplay();
  20. }
  21. private void FileValueLabel_DragEnter(object sender, DragEventArgs e)
  22. {
  23. e.Effect =
  24. e.Data.GetDataPresent(DataFormats.FileDrop) && e.Data.GetData(DataFormats.FileDrop) is string[] arr && arr.Length == 1 && File.Exists(arr[0])
  25. ? DragDropEffects.Link
  26. : DragDropEffects.None;
  27. }
  28. private void FileValueLabel_DragDrop(object sender, DragEventArgs e)
  29. {
  30. _selectedFile = FileValueLabel.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
  31. UpdateDisplay();
  32. }
  33. private void ExpandNumericUpDown_ValueChanged(object sender, EventArgs e)
  34. {
  35. UpdateDisplay();
  36. }
  37. private void UnitRadioButton_CheckedChanged(object sender, EventArgs e)
  38. {
  39. var self = (RadioButton)sender;
  40. if (self.Checked == false) return;
  41. UpdateDisplay();
  42. }
  43. private void GoButton_Click(object sender, EventArgs e)
  44. {
  45. if (null == _selectedFile)
  46. {
  47. MessageBox.Show("No file selected!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  48. return;
  49. }
  50. if (!File.Exists(_selectedFile))
  51. {
  52. MessageBox.Show("No such file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  53. return;
  54. }
  55. using var fs = new FileStream(_selectedFile, FileMode.Open, FileAccess.Write, FileShare.None);
  56. fs.SetLength(fs.Length + GetExpand());
  57. fs.Close();
  58. MessageBox.Show("Success");
  59. Close();
  60. }
  61. //---------------------------
  62. private void UpdateDisplay()
  63. {
  64. if (null == _selectedFile)
  65. {
  66. SizeValueLabel.Text = "⚠ No file selected";
  67. AfterExpandValueLabel.Text = "";
  68. return;
  69. }
  70. if (!File.Exists(_selectedFile))
  71. {
  72. SizeValueLabel.Text = "⚠ No such file";
  73. AfterExpandValueLabel.Text = "";
  74. return;
  75. }
  76. long size;
  77. try
  78. {
  79. //get real file size
  80. using var fs = new FileStream(_selectedFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  81. size = fs.Length;
  82. fs.Close();
  83. }
  84. catch (Exception e)
  85. {
  86. SizeValueLabel.Text = "⚠ Failure to get file size:" + e.Message;
  87. AfterExpandValueLabel.Text = "";
  88. return;
  89. }
  90. SizeValueLabel.Text = HumanReadableSize(size) + $" ( {size:N0} bytes )";
  91. var expand = GetExpand();
  92. if (expand == -1)
  93. {
  94. AfterExpandValueLabel.Text = "- No Unit Selected -";
  95. }
  96. else
  97. {
  98. var final = size + expand;
  99. AfterExpandValueLabel.Text = HumanReadableSize(final) + $" ( {final:N0} bytes )";
  100. }
  101. }
  102. private long GetExpand()
  103. {
  104. long expand = -1;
  105. if (KbRadioButton.Checked) expand = (long)ExpandNumericUpDown.Value * Kilobyte;
  106. else if (MbRadioButton.Checked) expand = (long)ExpandNumericUpDown.Value * Megabyte;
  107. else if (GbRadioButton.Checked) expand = (long)ExpandNumericUpDown.Value * Gigabyte;
  108. return expand;
  109. }
  110. private static string HumanReadableSize(long size)
  111. {
  112. var sizes = new[] { "B", "KB", "MB", "GB", "TB" };
  113. double len = size;
  114. var order = 0;
  115. while (len >= 1024 && order < sizes.Length - 1)
  116. {
  117. order++;
  118. len /= 1024;
  119. }
  120. // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
  121. // show a single decimal place, and no space.
  122. var result = String.Format("{0:0.##} {1}", len, sizes[order]);
  123. return result;
  124. }
  125. }
  126. }