Rani
Rani

Reputation: 3

Windows application (C#) - the process cannot access the file, because it is being used by another process

I am writing a winform application in C# to update the image.

Firstly I am checking whether the oldfilepath and newfilepath are similar or not, if not similar then I'm deleting the oldi mage and adding that new image to the created folder (named as "Image") and also updating new file path in database.

But when I'm clicking on update button it showing error like

"The process cannot access the file because it is being used by another process"

string oldpath;
if (File.Exists(Path.Combine(oldpath))) //oldpath where i'm getting the old file path
{
    File.Delete(Path.Combine(oldpath));
    if (File.Exists(Path.Combine(oldpath)))
    {
        File.Delete(Path.Combine(oldpath));
    }
}

Could anyone help me on this?

Upvotes: 0

Views: 79

Answers (1)

StefanaB
StefanaB

Reputation: 183

It is possible that the file mentioned in oldpath is actual open in another processes (for example, an notepad opened with it). Check if so, and close the process.

Upvotes: 1

Related Questions