復(fù)制文件夾(異步)時(shí),我需要顯示進(jìn)度. 我可以使用單個(gè)文件副本來執(zhí)行此操作,但不能使用文件夾來執(zhí)行此操作…我只想像Windows一樣顯示整個(gè)副本的進(jìn)度.
這是我復(fù)制文件夾的代碼:
private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
if (!Directory.Exists(destDirName))
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
sourceDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs, cts.Token);
}
}
}
然后通過一個(gè)按鈕調(diào)用它:
await Task.Run(() => DirectoryCopy(
srcFolder,
@"\\" hostname @"\C$\" destFolder @"\",
true,
cts.Token
));
我該如何實(shí)現(xiàn)?
告訴我有關(guān)我的問題的信息不足,我將更新我的帖子. 解決方法: 您可以使用IProgress接口.
例如,
private async Task DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs, CancellationToken cancellationToken,IProgress<int> progress)
{
// Do work
var percentageProgress = 0;
// percentageProgress = Calculate percentage
progress.Report(percentageProgress);
}
在客戶端(相信您的按鈕單擊事件),
var progressIndicator = new Progress<int>(ShowProgress);
await UploadPicturesAsync(sourceDirName,destDirName,copySubDirs,token,progressIndicator);
其中ShowProgress定義為
void ShowProgress(int value)
{
// Update UI
}
您也可以在IProgress here和here上閱讀更多內(nèi)容 來源:https://www./content-4-551751.html
|