3

Every time I save a file and delete it right away using the function below, I keep getting this error message: "System.IO.IOException: The process cannot access the file because it is being used by another process".

Waiting for a couple of minutes or closing visual studio seems to only unlock the files that you uploaded previously.

public static bool DeleteFiles(List<String> paths) { // Returns true on success try { foreach (var path in paths) { if (File.Exists(HostingEnvironment.MapPath("~") + path)) File.Delete(HostingEnvironment.MapPath("~") + path); } } catch (Exception ex) { return false; } return true; } 

I think that the way I'm saving the files may cause them to be locked. This is the code for saving the file:

 if (FileUploadCtrl.HasFile) { filePath = Server.MapPath("~") + "/Files/" + FileUploadCtrl.FileName; FileUploadCtrl.SaveAs(filePath) } 

When looking for an answer I've seen someone say that you need to close the streamReader but from what I understand the SaveAs method closes and disposes automatically so I really have no idea whats causing this

3
  • Check whether the file is open in another program? Commented Aug 12, 2022 at 10:01
  • 1
    Consider using FileOptions.DeleteOnClose as you can see here: stackoverflow.com/questions/3240968/… Commented Aug 12, 2022 at 10:03
  • Are you sure you closed the stream correctly with using? Commented Aug 12, 2022 at 10:57

2 Answers 2

1

I think that the problem is not about streamReader in here.

When you run the program, your program runs in a specific folder. Basically, That folder is locked by your program. In that case, when you close the program, it will be unlocked.

To fix the issue, I would suggest to write/delete/update to different folder.

Another solution could be to check file readOnly attribute and change this attribute which explained in here

Last solution could be using different users. What I mean is that, if you create a file with different user which not admin, you can delete with Admin user. However, I would definitely not go with this solution cuz it is too tricky to manage different users if you are not advance windows user.

Sign up to request clarification or add additional context in comments.

4 Comments

After some more debugging I've noticed that the issue only occurs with audio media files. I also tried to delete them manually and goy this error message: "this action cannot be completed because the file is open in IIS Express Worker Procces"
@Aiotex are you sure it has finished saving before you delete it? Are the audio files maybe larger than the other files, you have tried?
No, I'm uploading 3 second audio clips
So in that case IIS Express -- basically your application -- locks the file or folder. Have you tried to save file to different folder ?
1

After some testing, I found the problem. turns out I forgot about a function I made that was called every time I saved a media file. the function returned the duration of the file and used NAudio.Wave.WaveFileReader and NAudio.Wave.Mp3FileReader methods which I forgot to close after I called them

I fixed these issues by putting those methods inside of a using statement

Here is the working function:

public static int GetMediaFileDuration(string filePath) { filePath = HostingEnvironment.MapPath("~") + filePath; if (Path.GetExtension(filePath) == ".wav") using (WaveFileReader reader = new WaveFileReader(filePath)) return Convert.ToInt32(reader.TotalTime.TotalSeconds); else if(Path.GetExtension(filePath) == ".mp3") using (Mp3FileReader reader = new Mp3FileReader(filePath)) return Convert.ToInt32(reader.TotalTime.TotalSeconds); return 0; } 

The moral of the story is, to check if you are opening the file anywhere else in your project

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.