2

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

Aiotex
  • 53
  • 1
  • 7

2 Answers2

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.

ahmet gül
  • 193
  • 1
  • 11
  • 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 Aug 12 '22 at 10:41
  • 1
    @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? – asgerhallas Aug 12 '22 at 10:46
  • No, I'm uploading 3 second audio clips – Aiotex Aug 12 '22 at 10:50
  • 1
    So in that case IIS Express -- basically your application -- locks the file or folder. Have you tried to save file to different folder ? – ahmet gül Aug 12 '22 at 11:01
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

Aiotex
  • 53
  • 1
  • 7