Datei Kopieren fortschritt in Progressbar anzeigen

  • C#
  • .NET (FX) 4.0

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von ManuelSoftware.

    Datei Kopieren fortschritt in Progressbar anzeigen

    Hallo Community,
    mal eine kleine Frage ich hab schon den Code zum asynchronen Kopieren und auch zum darstellen in der ProgressBar aber ich hab gerade eine blockade und kann einfach nicht weiter rechnen, ich will in einer einzelnen ProgressBar den fortschritt anzeigen von allen Dateien:

    Mein Code
    Spoiler anzeigen

    C#-Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Runtime.InteropServices;
    8. using System.Text;
    9. using System.Threading.Tasks;
    10. using System.Windows.Forms;
    11. namespace Camera_Image_Saver
    12. {
    13. public partial class frmCopying : Form
    14. {
    15. public frmCopying()
    16. {
    17. InitializeComponent();
    18. }
    19. List<Copyer> copy = new List<Copyer>();
    20. int now = 0;
    21. int cs = 0;
    22. string CopyDir;
    23. bool hierarchy = false;
    24. string DestDir;
    25. public void AnalyzeDir(string dir)
    26. {
    27. foreach (var files in System.IO.Directory.GetFiles(dir))
    28. {
    29. cs++;
    30. string Destination;
    31. if (hierarchy)
    32. {
    33. Destination = System.IO.Path.Combine(DestDir, OnlyCDir(dir), System.IO.Path.GetFileName(files));
    34. }else
    35. {
    36. Destination = System.IO.Path.Combine(DestDir, System.IO.Path.GetFileName(files));
    37. }
    38. CIN(System.IO.Path.GetDirectoryName(Destination));
    39. if (System.IO.File.Exists(Destination))
    40. System.IO.File.Delete(Destination);
    41. Copyer c = new Copyer(files, Destination);
    42. copy.Add(c);
    43. }
    44. foreach (var dir2 in System.IO.Directory.GetDirectories(dir))
    45. { try
    46. {
    47. AnalyzeDir(dir2);
    48. }
    49. catch { }
    50. }
    51. }
    52. public void CIN(string dir)
    53. {
    54. if (!System.IO.Directory.Exists(dir))
    55. System.IO.Directory.CreateDirectory(dir);
    56. }
    57. List<Copyer> finishedFiles;
    58. bool paused = false;
    59. public bool Pause
    60. {
    61. get
    62. {
    63. return paused;
    64. }
    65. set
    66. {
    67. paused = value;
    68. if(value == false)
    69. {
    70. pgProgress.State = AeroSuite.Controls.ProgressBarState.Normal;
    71. CopyNext();
    72. }
    73. }
    74. }
    75. public void PrepareSettings()
    76. {
    77. finishedFiles = new List<Copyer>();
    78. copy = new List<Copyer>();
    79. this.Show();
    80. DisableCloseButton(this.Handle);
    81. }
    82. private void SetProgress(int value)
    83. {
    84. if (value == 0)
    85. return;
    86. pgProgress.Value = value;
    87. switch (value.ToString().Length)
    88. {
    89. case 1:
    90. lblPercentage.Text = value + " %";
    91. break;
    92. case 2:
    93. lblPercentage.Text = value + " %";
    94. break;
    95. case 3:
    96. lblPercentage.Text = value + "%";
    97. break;
    98. }
    99. lblPercentage.Refresh();
    100. Refresh();
    101. }
    102. public void PrepareStart()
    103. {
    104. SetLabelName(System.IO.Path.GetFileName(copy[0].SourceFilePath));
    105. pgProgress.State = AeroSuite.Controls.ProgressBarState.Normal;
    106. SetProgress(0);
    107. }
    108. public void SetLabelName(string str)
    109. {
    110. lblFile.Text = CopierStart.Replace("%file%", str);
    111. }
    112. public string OnlyCDir(string subdir)
    113. {
    114. string arx = subdir.Replace(CopyDir, "");
    115. if (arx.StartsWith("\\"))
    116. arx = arx.Remove(0, 1
    117. );
    118. return arx;
    119. }
    120. public void OpenCopy(string Directory, string DestDir, bool UseHierarchy = false)
    121. {
    122. CopyDir = Directory;
    123. this.DestDir = DestDir;
    124. PrepareSettings();
    125. //Analyzes Files
    126. AnalyzeDir(Directory);
    127. if (copy.Count == 0)
    128. FinishCopying();
    129. }
    130. private void FinishCopying()
    131. {
    132. lblPercentage.Text = "Fertig";
    133. MessageBox.Show("Files successfully copied from " + gsfs + CopyDir + gsfs + " to " + gsfs + DestDir + gsfs, "Camera Image Saver", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    134. this.Close();
    135. }
    136. string curFile;
    137. public void CopyNext()
    138. {
    139. if(paused)
    140. {
    141. pgProgress.State = AeroSuite.Controls.ProgressBarState.Paused;
    142. lblFile.Text = "paused";
    143. btnPC.Enabled = true;
    144. btnPC.Text = "Continue";
    145. Refresh();
    146. return;
    147. }
    148. now++;
    149. Copyer c = copy[now];
    150. SetLabelName(System.IO.Path.GetFileName(c.SourceFilePath));
    151. curFile = System.IO.Path.GetFileName(c.SourceFilePath);
    152. c.OnProgressChanged += C_OnProgressChanged;
    153. c.OnComplete += C_OnComplete;
    154. Refresh();
    155. c.Copy();
    156. }
    157. private void C_OnComplete()
    158. {
    159. if (now == copy.Count - 1)
    160. {
    161. FinishCopying();
    162. return;
    163. }
    164. finishedFiles.Add(copy[now]);
    165. CopyNext();
    166. }
    167. private void C_OnProgressChanged(double Persentage, ref bool Cancel)
    168. {
    169. //int validate = cs /
    170. int valueToAdd = (int)Math.Round(Persentage);
    171. int valval = cs / valueToAdd;
    172. if (valval + pgProgress.Value >= pgProgress.Maximum)
    173. {
    174. pgProgress.Value = pgProgress.Maximum;
    175. lblPercentage.Text = "100%";
    176. return;
    177. }
    178. AddProgress(valval);
    179. Refresh();
    180. }
    181. public void AddProgress(int val)
    182. {
    183. SetProgress(pgProgress.Value + val);
    184. }
    185. [DllImport("user32.dll")]
    186. private static extern IntPtr RemoveMenu(IntPtr hMenu, UInt32 uPosition, UInt32 uFlags);
    187. [DllImport("user32.dll")]
    188. private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
    189. [DllImport("user32.dll")]
    190. private static extern int GetMenuItemCount(IntPtr hMenu);
    191. [DllImport("user32.dll")]
    192. private static extern bool DrawMenuBar(IntPtr hwnd);
    193. private void DisableCloseButton(IntPtr hwnd)
    194. {
    195. IntPtr hMenu = GetSystemMenu(hwnd, false);
    196. int menuItemCount = GetMenuItemCount(hMenu);
    197. RemoveMenu(hMenu, (uint)(menuItemCount - 1), 0x2 | 0x400);
    198. DrawMenuBar(hwnd);
    199. }
    200. private void EnableCloseButton(IntPtr hwnd)
    201. {
    202. GetSystemMenu(hwnd, true);
    203. DrawMenuBar(hwnd);
    204. }
    205. public const string CopierStart = "Copy File \"%file%\"";
    206. public const char gsfs = '"';
    207. private void frmCopying_Load(object sender, EventArgs e)
    208. {
    209. }
    210. private void btnPC_Click(object sender, EventArgs e)
    211. {
    212. switch(btnPC.Text.ToLower())
    213. {
    214. default:
    215. case "pause":
    216. Pause = true;
    217. btnPC.Enabled = false;
    218. break;
    219. case "continue":
    220. Pause = false;
    221. btnPC.Enabled = true;
    222. btnPC.Text = "Pause";
    223. break;
    224. case "start":
    225. captionLabel.Text = "Please wait, Files copying";
    226. PrepareStart();
    227. Refresh();
    228. //Start Copy
    229. btnPC.Text = "Pause";
    230. now = -1;
    231. btnPC.Visible = false;
    232. CopyNext();
    233. break;
    234. }
    235. }
    236. }
    237. }



    In dem Bereich hängts:

    C#-Quellcode

    1. private void C_OnProgressChanged(double Persentage, ref bool Cancel)
    2. {
    3. int valueToAdd = (int)Math.Round(Persentage);
    4. int valval = cs / valueToAdd;
    5. if (valval + pgProgress.Value >= pgProgress.Maximum)
    6. {
    7. pgProgress.Value = pgProgress.Maximum;
    8. lblPercentage.Text = "100%";
    9. return;
    10. }
    11. AddProgress(valval);
    12. Refresh();
    13. }

    Ich bekomm das einfach nicht in die Anzahl der Dateien und den Fortschritt in der ProgressBar darzustellen. Vor einer halben ewigkeit hab ich das schonmal gemacht, da hab ich aber den MaxValue bei der Progressbar so hoch gesetzt das alle Dateien 100 % zur verfügung haben nun will ich das aber so runterrechnen das alles nur auf die 100 % sind, falls ihr es nicht verstanden habt tuts mir leid ich versteh ni wie ich es ausdrücken soll

    Spoiler eingefügt. ~Trade

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Trade“ ()

    1. Try-Catches ohne Behandlung.
    2. Es heißt Percentage.
    3. Coderedundanz und nicht eingehaltene Guidelines überall.

    Solltest Du ändern.

    Ansonsten, wo ist das Problem? Ich steige bei dem Code irgendwie nicht durch, der ist viel zu komplex aufgebaut. Mach doch einfach die aktuelle Anzahl durch die gesamte und dann mal 100. Vorsicht, welch Datentypen du dabei nutzt! Mit int wirst Du dann die Zahl nicht korrekt darstellen können.

    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!: