I had the below scenarios which was causing the similar error:
· While uploading files to the server
· While getting rid of my old files after they are uploaded to the server.
Most of the files were small in size, however, a few were very large, and so while attempting to delete my files resulted in cannot access file error.
It was not easy for me to find it, but the solution for it was as simple as Waiting "for my task to complete the execution":
using (var mywc = new WebClient())
var mytskResult = mywc.UploadFileTaskAsync(_address, _fileName);
I also had faced the same issue in the recent past while I was trying to upload my image and I couldn't delete it and found the solution later on it.
//The C# .NET
var myimage = Image.FromFile(filePath);
myimage.Dispose(); // this will remove all resources
File.Delete(filePath); //now it works