I'm trying to copy a list of files to a directory. I'm using async / await. But I've been getting this compilation error
The 'await' operator can only be used within an async lambda expression. Consider marking this lambda expression with the 'async' modifier.
This is what my code looks like
async Task<int> CopyFilesToFolder(List<string> fileList,
IProgress<int> progress, CancellationToken ct)
{
int totalCount = fileList.Count;
int processCount = await Task.Run<int>(() =>
{
int tempCount = 0;
foreach (var file in fileList)
{
string outputFile = Path.Combine(outputPath, file);
await CopyFileAsync(file, outputFile); //<-- ERROR: Compilation Error
ct.ThrowIfCancellationRequested();
tempCount++;
if (progress != null)
{
progress.Report((tempCount * 100 / totalCount)));
}
}
return tempCount;
});
return processCount;
}
private async Task CopyFileAsync(string sourcePath, string destinationPath)
{
using (Stream source = File.Open(sourcePath, FileMode.Open))
{
using (Stream destination = File.Create(destinationPath))
{
await source.CopyToAsync(destination);
}
}
}
Pls can anyone point out what am I missing here ?