0
 System.IO.File.Delete(BmpPath);

I have a problem with delete file.

"The process cannot access the file 'xxxx' because it is being used by another proces".

Howw can I solve my problem?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

3 Answers3

2

First, try to determine if the file is used by another process.

Do that using Task Manager, or a third party, like Process Explorer.

If you cannot determine using those, also try Handle.

Handle is targetted at searching for open file references, so if you do not specify any command-line parameters it will list the values of all the handles in the system that refer to open files and the names of the files. It also takes several parameters that modify this behavior.

Ultimately, in the unlikely case that none of these work, you might try rebooting your machine and see if the problem occurs again.

In that case, the culprit process is most probably started at startup, and you can continue searching with that in mind.

nestedloop
  • 2,596
  • 23
  • 34
  • A nice alternative to determine who is locking the file is [Unlocker](http://www.majorgeeks.com/files/details/unlocker.html). It has an intuitive GUI and Windows integration, so you cand just right-click the problematic file and use Unlocker to find out the problem. – ClotzA Dec 20 '13 at 08:40
0

Your file is locked, Close your file if it is opened or in use by some other application.

If another process has the file locked, you will not be allowed to delete the file until that process has released the file.

IOException

Also check for whether file exists or not.

try using

 if(System.IO.File.Exists(@"xxxxxxx\test.txt"))
        {
            // Use a try block to catch IOExceptions, to 
            // handle the case of the file already being 
            // opened by another process. 
            try
            {
                System.IO.File.Delete(@"xxxxxxxx\test.txt");
            }
            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
                return;
            }
        }
Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
  • I haven't got a access to the file. – user3084365 Dec 20 '13 at 08:54
  • @user3084365 I am sorry didn't get your previous comment.Can you give some more info on your question ? – Suraj Singh Dec 20 '13 at 09:03
  • C# Bitmap bmp=(Bitmap)Bitmap.FromFile(FilePath); pic.Source=BitmapImage(bmp); File.Delete(FilePath); XAML (BitmapImage is a function which return a BitmapImage from Bitmap) On File.Delete(FilePath) show me the problem. – user3084365 Dec 20 '13 at 09:05
  • @user3084365 So your path is inaccessible and as per your previous comments you also loading image, that may be causing locking on your resource, can you add some details in your question. – Suraj Singh Dec 20 '13 at 09:13
0

Try checking whether the file is open in some program or not. If it is then close the program and then try running your code. Otherwise restart your PC and then try again.

HelloUni
  • 448
  • 2
  • 5
  • 10