MainForm.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Windows.Forms;
  5. namespace StrangeFileCopy
  6. {
  7. public partial class MainForm : Form
  8. {
  9. private string _status = "Ready";
  10. private int _bufProgress;
  11. private int _reOpenProgress;
  12. private int _totalProgress;
  13. public MainForm()
  14. {
  15. InitializeComponent();
  16. }
  17. private void FireButton_Click(object sender, EventArgs e)
  18. {
  19. var srcPath = FromTextBox.Text;
  20. var dstPath = ToTextBox.Text;
  21. if (string.IsNullOrWhiteSpace(srcPath) || string.IsNullOrWhiteSpace(dstPath))
  22. {
  23. MessageBox.Show("Before start,fill `File From' and `To Folder'");
  24. return;
  25. }
  26. var blkSize = MaxCheckBox.Checked ? 2147483591 : (int)BufSizeUpDown.Value * 1024 * 1024;
  27. var reOpenTimes = (int)Math.Ceiling(ReOpenSizeUnDown.Value * 1024 / BufSizeUpDown.Value);
  28. var flush = FlushCheckBox.Checked;
  29. FireButton.Enabled = false;
  30. ConfigGroupBox.Enabled = false;
  31. new Thread(() =>
  32. {
  33. FileStream src = null;
  34. FileStream dst = null;
  35. try
  36. {
  37. _status = "Opening Source";
  38. src = File.OpenRead(srcPath);
  39. var srcLen = src.Length;
  40. long pos = 0;
  41. var buf = new byte[blkSize];
  42. while (pos < srcLen)
  43. {
  44. //open dest and set position
  45. _status = "Opening Dest";
  46. dst = File.OpenWrite(dstPath);
  47. dst.Position = pos;
  48. for (var i = 0; i < reOpenTimes; i++)
  49. {
  50. _status = "Reading Source";
  51. var bi = 0;
  52. var read = 0;
  53. do
  54. {
  55. read = src.Read(buf, bi, blkSize - read);
  56. bi += read;
  57. _bufProgress = (int)(100 * (float)bi / blkSize);
  58. } while (read > 0 && bi < blkSize);
  59. _status = "Writing Dst";
  60. dst.Write(buf, 0, bi);
  61. _status = "Flushing Dst";
  62. if (flush) dst.Flush(true);
  63. _reOpenProgress = (int)(100 * (float)i / reOpenTimes);
  64. _totalProgress = (int)(100 * (float)src.Position / srcLen);
  65. if (src.Position == srcLen) break;
  66. }
  67. pos = src.Position;
  68. _status = "Closing Dst";
  69. dst.Close();
  70. }
  71. _status = "Done";
  72. }
  73. catch (Exception exception)
  74. {
  75. _status = $"Error:{exception.Message}";
  76. }
  77. finally
  78. {
  79. src?.Close();
  80. dst?.Close();
  81. }
  82. }).Start();
  83. }
  84. private void UiUpdateTimer_Tick(object sender, EventArgs e)
  85. {
  86. CurrentOperationLabel.Text = _status;
  87. BufProgressBar.Value = _bufProgress;
  88. ReOpenProgressBar.Value = _reOpenProgress;
  89. TotalProgressBar.Value = _totalProgress;
  90. }
  91. private void FromTextBox_DragEnter(object sender, DragEventArgs e)
  92. {
  93. string[] files;
  94. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop)
  95. && (files = (string[])e.Data.GetData(DataFormats.FileDrop)).Length == 1
  96. && File.Exists(files[0])
  97. ? DragDropEffects.Copy
  98. : DragDropEffects.None;
  99. }
  100. private void FromTextBox_DragDrop(object sender, DragEventArgs e)
  101. {
  102. FromTextBox.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
  103. }
  104. private void ToTextBox_DragEnter(object sender, DragEventArgs e)
  105. {
  106. string[] files;
  107. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop)
  108. && (files = (string[])e.Data.GetData(DataFormats.FileDrop)).Length == 1
  109. && Directory.Exists(files[0])
  110. ? DragDropEffects.Copy
  111. : DragDropEffects.None;
  112. }
  113. private void ToTextBox_DragDrop(object sender, DragEventArgs e)
  114. {
  115. var to = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
  116. new Thread(() =>
  117. {
  118. Invoke(new Action(delegate
  119. {
  120. if (string.IsNullOrWhiteSpace(FromTextBox.Text))
  121. {
  122. MessageBox.Show("Fill `File from' first!");
  123. return;
  124. }
  125. var filename = Path.GetFileName(FromTextBox.Text);
  126. var dest = Path.Combine(to, filename);
  127. if (File.Exists(dest))
  128. {
  129. if (DialogResult.Yes == MessageBox.Show("Target file exist,override it?"))
  130. {
  131. ToTextBox.Text = dest;
  132. }
  133. else
  134. {
  135. ToTextBox.Clear();
  136. }
  137. }
  138. else
  139. {
  140. ToTextBox.Text = dest;
  141. }
  142. }));
  143. }).Start();
  144. }
  145. private void MaxCheckBox_CheckedChanged(object sender, EventArgs e)
  146. {
  147. BufSizeUpDown.Enabled = !MaxCheckBox.Checked;
  148. }
  149. }
  150. }