System.IO.File.Delete throws "The process cannot access the file because it is being used by another process"

Viewed 190

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

2 Answers

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

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.

Related