kAsdh
kAsdh

Reputation: 366

How to detect file copying process has finished in C#

I am working on a project that I need to do the following: I need to rename an image file. (Open an image from folder, and give a name & save it in to same folder)

try
{
    string oldFileName = @"path\to\person1.jpg";
    string desFileName = @"path\to\person2.jpg";

    File.Copy(oldFileName, desFileName, true);

    if (File.Exists(oldFileName))
    {
        File.Delete(@oldFileName);
    }
}
catch (Exception ex)
{
}

I did rename the file using above way.

This process copy the old file with new name, but couldn't remove old file

Exception message :

The process cannot access the file 'path\to\person1.jpg' because it is being used by another process.

How to resolve this? Please suggest any way to detect copying process has complete or not.

Upvotes: 0

Views: 648

Answers (1)

teo van kot
teo van kot

Reputation: 12491

Your copy process is definatly complete on if statement becouse your code is sync.

I bet you got this error becouse file is used by another proccess (not your programm). Maby you have paint open or something else.

You should find it out with process monitor or something else. Check this question.

Upvotes: 2

Related Questions