提问人:Minesh 提问时间:10/30/2023 最后编辑:Palle DueMinesh 更新时间:10/30/2023 访问量:52
带进度条的异步文件夹复制
Async folder copy with progress bar
问:
我在网上搜索并找到了 c# 代码来复制带有进度条的异步文件,该代码运行良好。现在我正在尝试复制一个包含多个文件的文件夹。我添加了一个额外的进度条,但新的进度条无需等待即可快速完成。
我把代码放在这里:
private void button1_Click(object sender, EventArgs e)
{
var _source = txtSource.Text;
var _destination = txtDestination.Text;
Task.Run(() =>
{
CopyFoldersRecursively(_source, _destination, x => progressBar1.BeginInvoke(new Action(() => { progressBar1.Value = x; })));
}).GetAwaiter().OnCompleted(() => progressBar1.BeginInvoke(new Action(() => { progressBar1.Value = 100; })));
}
public void CopyFoldersRecursively(string sourcePath, string targetPath, Action<int> progressCallback)
{
try
{
// Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
}
// Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
{
var _source = new FileInfo(newPath);
var _destination = new FileInfo(targetPath + @"\" + Path.GetFileName(_source.ToString()));
Task.Run(() =>
{
_source.CopyTo(_destination, x => progressBar2.BeginInvoke(new Action(() => { progressBar2.Value = x; lblPercent2.Text = x.ToString() + "%"; })));
}).GetAwaiter().OnCompleted(() => progressBar2.BeginInvoke(new Action(() => { progressBar2.Value = 100; lblPercent2.Text = "100%"; })));
}
}
catch (Exception ex)
{
throw ex;
}
}
public static class FileInfoExtension
{
public static void CopyTo(this FileInfo file, FileInfo destination, Action<int> progressCallback)
{
const int bufferSize = 1024 * 1024;
byte[] buffer = new byte[bufferSize], buffer2 = new byte[bufferSize];
bool swap = false;
int progress = 0, reportedProgress = 0, read = 0;
long len = file.Length;
float flen = len;
Task writer = null;
using (var source = file.OpenRead())
using (var dest = destination.OpenWrite())
{
dest.SetLength(source.Length);
for (long size = 0; size < len; size += read)
{
if ((progress = ((int)((size / flen) * 100))) != reportedProgress)
progressCallback(reportedProgress = progress);
read = source.Read(swap ? buffer : buffer2, 0, bufferSize);
writer?.Wait();
writer = dest.WriteAsync(swap ? buffer : buffer2, 0, read);
swap = !swap;
}
writer?.Wait();
}
}
}
答: 暂无答案
评论
await Task.Run(() => _source.CopyTo(...
await CopyFoldersRecursively(...
CopyFoldersRecursively()
async Task
void
Task.Run(() =>
button1_Click()
Task.Run(async () =>
Progress<T>