The Process Cannot Access the File Error in C#

This error message mostly comes up when you try to access a file that is opened by another process. You may open an image file in one of your forms in a PictureBox using ImageFromFile or something. I mostly use a memory stream to open an image. After reading it in byte[] write it into a stream and close it.

You can check whether a file is being used or not with a simple function.

Try to open a file with none share.

 public bool IsFileUsedbyAnotherProcess(string filename)

            File.Open(filename), FileMode.Open, FileAccess.Read, FileShare.None);
        catch (System.IO.IOException exp)
            return true;
        return false;

This function will return true if the file because is being used by another process or not.

Next Post Previous Post
No Comment
Add Comment
comment url