I am a wpf newb so this question may be trivial. I am trying to copy a file from one folder to another. I would like to show a progressbar during the copy process.
My code is like this:
if (!System.IO.File.Exists(basemapDest))
{
await Copier.CopyFiles(new Dictionary<string, string>
{
{basemapSrc, basemapDest},
}, prog => prgBaseMap.Value = prog);
}
public static class Copier
{
public static async Task CopyFiles(Dictionary<string, string> files, Action<int> progressCallback)
{
for (var x = 0; x < files.Count; x++)
{
var item = files.ElementAt(x);
var from = item.Key;
var to = item.Value;
using (var outStream = new FileStream(to, FileMode.Create, FileAccess.Write, FileShare.Read))
{
using (var inStream = new FileStream(from, FileMode.Open, FileAccess.Read, FileShare.Read))
{
long fileLength = from.Length;
await inStream.CopyToAsync(outStream);
}
}
progressCallback((int)((x + 1) / files.Count) * 100);
}
}
}
My XAML Code:
<StackPanel>
<ProgressBar x:Name="prgBaseMap" Height="10" Visibility="Collapsed"/>
</StackPanel>
While this works for reporting a file is copied it doesn't show progress while I am doing the copy. What am I doing wrong ?
*** Edit, this is not a copy of stream.copyto with progress bar reporting
the referenced question is using a BackgroundWorker which these days is considered by many people to be obsolete. This question is about using the new asynchronous model of .NET. I hope the provided solution proves useful to others as well.