FileExpanderForm.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Remoting.Messaging;
  4. using System.Windows.Forms;
  5. using File = System.IO.File;
  6. namespace FileExpander
  7. {
  8. public partial class FileExpanderForm : Form
  9. {
  10. private const int Kilobyte = 1024;
  11. private const int Megabyte = Kilobyte * 1024;
  12. private const int Gigabyte = Megabyte * 1024;
  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. 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. FileValueLabel.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
  31. UpdateDisplay();
  32. }
  33. private void UnitRadioButton_CheckedChanged(object sender, EventArgs e)
  34. {
  35. var self = (RadioButton)sender;
  36. if (self.Checked == false) return;
  37. UpdateDisplay();
  38. }
  39. private void GoButton_Click(object sender, EventArgs e)
  40. {
  41. using var fs = new FileStream(FileValueLabel.Text, FileMode.Open, FileAccess.Write, FileShare.None);
  42. fs.SetLength(fs.Length + GetExpand());
  43. fs.Close();
  44. MessageBox.Show("Success");
  45. Close();
  46. }
  47. //---------------------------
  48. private void UpdateDisplay()
  49. {
  50. var file = FileValueLabel.Text;
  51. if (!File.Exists(file))
  52. {
  53. SizeValueLabel.Text = "⚠ No such file";
  54. AfterExpandValueLabel.Text = "";
  55. return;
  56. }
  57. long size;
  58. try
  59. {
  60. //get real file size
  61. using var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  62. size = fs.Length;
  63. fs.Close();
  64. }
  65. catch (Exception e)
  66. {
  67. SizeValueLabel.Text = "⚠ Failure to get file size:" + e.Message;
  68. AfterExpandValueLabel.Text = "";
  69. return;
  70. }
  71. SizeValueLabel.Text = HumanReadableSize(size) + $" ( {size:N0} bytes )";
  72. var expand = GetExpand();
  73. if (expand == -1)
  74. {
  75. AfterExpandValueLabel.Text = "- No Unit Selected -";
  76. }
  77. else
  78. {
  79. var final = size + expand;
  80. AfterExpandValueLabel.Text = HumanReadableSize(final) + $" ( {final:N0} bytes )";
  81. }
  82. }
  83. private long GetExpand()
  84. {
  85. long expand = -1;
  86. if (KbRadioButton.Checked) expand = (long)ExpandNumericUpDown.Value * Kilobyte;
  87. else if (MbRadioButton.Checked) expand = (long)ExpandNumericUpDown.Value * Megabyte;
  88. else if (GbRadioButton.Checked) expand = (long)ExpandNumericUpDown.Value * Gigabyte;
  89. return expand;
  90. }
  91. private static string HumanReadableSize(long size)
  92. {
  93. var sizes = new[] { "B", "KB", "MB", "GB", "TB" };
  94. double len = size;
  95. var order = 0;
  96. while (len >= 1024 && order < sizes.Length - 1)
  97. {
  98. order++;
  99. len /= 1024;
  100. }
  101. // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
  102. // show a single decimal place, and no space.
  103. var result = String.Format("{0:0.##} {1}", len, sizes[order]);
  104. return result;
  105. }
  106. }
  107. }