MainForm.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using Microsoft.WindowsAPICodePack.Taskbar;
  2. using System;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. namespace StrangeFileCopy
  7. {
  8. public partial class MainForm : Form
  9. {
  10. private string _status = "Ready";
  11. private int _bufProgress;
  12. private int _reOpenProgress;
  13. private int _totalProgress;
  14. private bool _pause;
  15. private bool _error;
  16. public MainForm() => InitializeComponent();
  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 * 1024 * 1024 / blkSize);
  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. _status = "Pre allocating Dest - Opening";
  41. dst = File.OpenWrite(dstPath);
  42. _status = "Pre allocating Dest - Seeking";
  43. dst.Position = src.Length - 1;
  44. _status = "Pre allocating Dest - Writing";
  45. dst.WriteByte(0);
  46. _status = "Pre allocating Dest - Flushing";
  47. dst.Flush(true);
  48. _status = "Pre allocating Dest - Closing";
  49. dst.Close();
  50. long pos = 0;
  51. var buf = new byte[blkSize];
  52. while (pos < srcLen)
  53. {
  54. _status = "Opening Dest";
  55. dst = File.OpenWrite(dstPath);
  56. dst.Position = pos;
  57. for (var i = 0; i < reOpenTimes; i++)
  58. {
  59. _status = "Reading Source";
  60. var bi = 0;
  61. var read = 0;
  62. do
  63. {
  64. read = src.Read(buf, bi, blkSize - read);
  65. bi += read;
  66. _bufProgress = (int)(100 * (float)bi / blkSize);
  67. } while (read > 0 && bi < blkSize);
  68. _status = "Writing Dst";
  69. dst.Write(buf, 0, bi);
  70. _status = "Flushing Dst";
  71. if (flush) dst.Flush(true);
  72. _reOpenProgress = (int)(100 * (float)i / reOpenTimes);
  73. _totalProgress = (int)(100 * (float)src.Position / srcLen);
  74. if (_pause)
  75. {
  76. _status = "Paused";
  77. while (_pause)
  78. {
  79. Thread.Sleep(250);
  80. }
  81. }
  82. if (src.Position == srcLen) break;
  83. }
  84. pos = src.Position;
  85. _status = "Closing Dst";
  86. dst.Close();
  87. }
  88. _status = "Done";
  89. }
  90. catch (Exception exception)
  91. {
  92. _status = $"Error:{exception.Message}";
  93. _error = true;
  94. }
  95. finally
  96. {
  97. src?.Close();
  98. try
  99. {
  100. dst?.Close();
  101. }
  102. catch
  103. {
  104. //ignore
  105. }
  106. }
  107. }).Start();
  108. }
  109. private void UiUpdateTimer_Tick(object sender, EventArgs e)
  110. {
  111. CurrentOperationLabel.Text = _status;
  112. BufProgressBar.Value = _bufProgress;
  113. ReOpenProgressBar.Value = _reOpenProgress;
  114. TotalProgressBar.Value = _totalProgress;
  115. if (_error)
  116. {
  117. TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Error);
  118. }
  119. else
  120. {
  121. TaskbarManager.Instance.SetProgressState(_pause ? TaskbarProgressBarState.Paused : TaskbarProgressBarState.Normal);
  122. TaskbarManager.Instance.SetProgressValue(_totalProgress, 100);
  123. }
  124. }
  125. private void FromTextBox_DragEnter(object sender, DragEventArgs e)
  126. {
  127. string[] files;
  128. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop)
  129. && (files = (string[])e.Data.GetData(DataFormats.FileDrop)).Length == 1
  130. && File.Exists(files[0])
  131. ? DragDropEffects.Copy
  132. : DragDropEffects.None;
  133. }
  134. private void FromTextBox_DragDrop(object sender, DragEventArgs e) => FromTextBox.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
  135. private void ToTextBox_DragEnter(object sender, DragEventArgs e)
  136. {
  137. string[] files;
  138. e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop)
  139. && (files = (string[])e.Data.GetData(DataFormats.FileDrop)).Length == 1
  140. && Directory.Exists(files[0])
  141. ? DragDropEffects.Copy
  142. : DragDropEffects.None;
  143. }
  144. private void ToTextBox_DragDrop(object sender, DragEventArgs e)
  145. {
  146. var to = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
  147. new Thread(() =>
  148. {
  149. Invoke(new Action(delegate
  150. {
  151. if (string.IsNullOrWhiteSpace(FromTextBox.Text))
  152. {
  153. MessageBox.Show("Fill `File from' first!");
  154. return;
  155. }
  156. var filename = Path.GetFileName(FromTextBox.Text);
  157. var dest = Path.Combine(to, filename);
  158. if (File.Exists(dest))
  159. {
  160. if (DialogResult.Yes == MessageBox.Show("Target file exist,override it?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2))
  161. {
  162. ToTextBox.Text = dest;
  163. }
  164. else
  165. {
  166. ToTextBox.Clear();
  167. }
  168. }
  169. else
  170. {
  171. ToTextBox.Text = dest;
  172. }
  173. }));
  174. }).Start();
  175. }
  176. private void MaxCheckBox_CheckedChanged(object sender, EventArgs e) => BufSizeUpDown.Enabled = !MaxCheckBox.Checked;
  177. private void PauseCheckBox_CheckedChanged(object sender, EventArgs e) => _pause = PauseCheckBox.Checked;
  178. }
  179. }