• Register
0 votes
176 views

Problem :

I have some of the code and when I try to executes it, it throws the below IOException, saying that

“The process cannot access the file 'filename' because it is being used by another process”

What is the meaning of this exception , and how can I completely fix the exception?

7.5k points
edited by

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

 

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);

  mytskResult.Wait();

}

 

OR

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

//later on...

File.Delete(filePath); //now it works

 

38.6k points