参考给出的方案
先求出文件和目录的数目:
1 2 3 4 5 6 7 8 9 10
| static int Count(String src) { int count = 1; String[] sub = Directory.GetDirectories(src); for (int i = 0; i < sub.Length; i++) { count += Count(sub[i]); } String[] files = Directory.GetFiles(src); count += files.Length; return count; }
|
设置ProgressBar属性Maximum为上面求出的数目,然后复制:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| static void Copy(String src, String dest) { try{ Directory.CreateDirectory(dest); String[] sub = Directory.GetDirectories(src); for (int i = 0; i < sub.Length; i++) { String name = Path.GetFileName(sub[i]); Copy(sub[i], dest + "\\" + name); } String[] files = Directory.GetFiles(src); for (int i = 0; i < files.Length; i++) { String name = Path.GetFileName(files[i]); File.Copy(files[i], dest + "\\" + name); } progressBar1.Increment(files.Length + 1); }catch(Exception e){ Console.WriteLine(e); } }
|
拷贝文件夹:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| static public void CopyDirectory(string SourceDirectory, string TargetDirectory) { DirectoryInfo source = new DirectoryInfo(SourceDirectory); DirectoryInfo target = new DirectoryInfo(TargetDirectory);
if(!source.Exists) return;
if(!target.Exists) target.Create();
FileInfo[] sourceFiles = source.GetFiles(); for(int i = 0; i < sourceFiles.Length; ++i) File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name,true);
DirectoryInfo[] sourceDirectories = source.GetDirectories(); for(int j = 0; j < sourceDirectories.Length; ++j) CopyDirectory(sourceDirectories[j].FullName,target.FullName +"\\" + sourceDirectories }
|
自己最后实现的进度条显示方法的部分代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
| static int Count(String src) { int count = 1; String[] sub = Directory.GetDirectories(src); for (int i = 0; i < sub.Length; i++) { count += Count(sub[i]); } String[] files = Directory.GetFiles(src); count += files.Length; return count; }
private void copyDir(string srcPath, string destPath) { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) { if (!Directory.Exists(destPath + "\\" + i.Name)) { Directory.CreateDirectory(destPath + "\\" + i.Name); } copyDir(i.FullName, destPath + "\\" + i.Name); } else { CopyFile_1(i.FullName, destPath + "\\" + i.Name, 1024); } FileCnt++;
if (this.InvokeRequired) { this.Invoke(new MethodInvoker(delegate { this.progressBar2.Maximum = FileMax; this.progressBar2.Value = FileCnt; this.label2.Text = (FileCnt*100 / FileMax).ToString() + "%";
this.richTextBox1.AppendText(FileCnt.ToString() + ":" + destPath + "\\" + i.Name + "\n"); this.richTextBox1.SelectionStart = this.richTextBox1.Text.Length; this.richTextBox1.ScrollToCaret();
} )); } } }
public void CopyFile_1(string FormerFile, string toFile, int SectSize) { FileStream fileToCreate = new FileStream(toFile, FileMode.Create); fileToCreate.Close(); fileToCreate.Dispose(); FormerOpen = new FileStream(FormerFile, FileMode.Open, FileAccess.Read); ToFileOpen = new FileStream(toFile, FileMode.Append, FileAccess.Write); int max = Convert.ToInt32(Math.Ceiling((double)FormerOpen.Length / (double)SectSize));
if (this.InvokeRequired) { this.Invoke(new MethodInvoker(delegate { this.progressBar1.Value = 0; this.progressBar1.Minimum = 0; this.progressBar1.Maximum = max; } )); }
int FileSize; if (SectSize < FormerOpen.Length) { byte[] buffer = new byte[SectSize]; int copied = 0; int tem_n = 1; while (copied <= ((int)FormerOpen.Length - SectSize)) { FileSize = FormerOpen.Read(buffer, 0, SectSize); FormerOpen.Flush(); ToFileOpen.Write(buffer, 0, SectSize); ToFileOpen.Flush(); ToFileOpen.Position = FormerOpen.Position; copied += FileSize;
if (this.InvokeRequired) { this.Invoke(new MethodInvoker(delegate { this.progressBar1.Value = this.progressBar1.Value + tem_n;
label1.Text = (copied / (FormerOpen.Length / 100)).ToString() + "%"; } )); }
} int left = (int)FormerOpen.Length - copied; FileSize = FormerOpen.Read(buffer, 0, left); FormerOpen.Flush(); ToFileOpen.Write(buffer, 0, left); ToFileOpen.Flush(); } else { byte[] buffer = new byte[FormerOpen.Length]; FormerOpen.Read(buffer, 0, (int)FormerOpen.Length); FormerOpen.Flush(); ToFileOpen.Write(buffer, 0, (int)FormerOpen.Length); ToFileOpen.Flush(); } FormerOpen.Close(); ToFileOpen.Close();
if (this.InvokeRequired) { this.Invoke(new MethodInvoker(delegate { this.progressBar1.Value = max; label1.Text = "100%"; } )); }
}
private void button5_Click(object sender, EventArgs e) { string path = textBox1.Text; string dest = textBox2.Text;
timer1.Start(); dt1 = DateTime.Parse("00:00:00"); Lab_Time.Text = "0:00:00";
Thread copyFilesThread = new Thread(() => { FileCnt = 1; if (File.Exists(path)) { string name = System.IO.Path.GetFileName(path); Console.WriteLine("是文件{0}", path); CopyFile_1(path, dest+"\\"+ name, 1024); } else if (Directory.Exists(path)) { Console.WriteLine("是文件夹"); FileMax = Count(path); copyDir(path, dest); } else { Console.WriteLine("都不是"); }
timer1.Stop(); MessageBox.Show("复制完成!");
}); copyFilesThread.Start();
}
|
相关链接(侵删)
- 如何用C#实现文件及文件夹复制,且显示复制的进度条
=================我是分割线=================
欢迎到公众号来唠嗑: