简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM