Delete Access Denied!!


I'm using the codes below trying to delete files and I got an error that say I'm trying to delete a file that still being used by another process. Please help me how to check which files are being use and skip over that and delete everything else. Here is my code so far, thanks in advance.

foreach (DirectoryInfo Dinfo in dirInfo.GetDirectories("*"))

{

foreach (FileInfo file in Dinfo.GetFiles("*.*"))

{

try

{

file.Delete();

}

catch (FileNotFoundException e)

{

Console.Write(e.FileName.ToString());

}

}

}

Thanks

Jason




Answer this question

Delete Access Denied!!

  • pamike

    Psst: IOException.

  • mahalax

    Yes, I try that and it doesn't delete the files in the "Temporary Internet Files" folder.



  • crogenicdude

    Try:


    List<FileInfo> MyFileList = new List<FileInfo>;

    foreach (DirectoryInfo Dinfo in dirInfo.GetDirectories("*"))

                     foreach (FileInfo file in Dinfo.GetFiles("*.*"))

                     {

                        try

                        {

                          MyFileList.Add(file);

                        }

                       catch (FileNotFoundException e)

                        {

                            Console.Write(e.FileName.ToString());

                         }

                      }


    foreach(FileInfo f in MyFileList) f.Delete();

    MyFileList.Clear();

     


    Maybe the first bucle is lock its contains files.


    Regards.



  • Madhu Gandhi

    Thanks Mark, I swear I changed it before to IOException! Think the topic title was stuck in my mind but the fingers were typing something else. So yes, you can implement both, the IOException and the UnAuthorizedException, in case you get the security error too

  • Dietz

    same code would apply as you have done, except point to the Temp internet files folder and delete the files it can delete. So are you saying you did this and its not deleting it

  • Polleveys

    some files you cannot delete in that folder as its a Windows folder and it manages it in its own way. Some files maybe in use, some are system files which Windows will deny access to

  • BillyB

    it wouldnt be a fileNotFound exception but rather an UnAuthorizedException in the catch block

    so overall the code:

    foreach(DirectoryInfo dInfo in dirInfo.GetDirectories("*"))

    {

       string[] theFiles = System.IO.Directory.GetFiles(dInfo.FullName);

       foreach(string currentFile in theFiles)

       {

          try

          {

             System.IO.File.Delete(currentFile);

          }

          catch (UnAuthorizedAccessException ex)

          {

             //handle if you like

          }

          catch (IOException ex)

          {

             //handle if you like

          }

       }

    }



  • Michael Dyrnaes

    Hi,

    Thanks for the info......but I have some .wma in the "Temporary Internet Files" folder and it not being use by any process nor it a Window file, it just some file downloaded from the internet. I can right click and delete it, but I want to write a C# code that will delete those files in my "Temporary Internet Files" folder. Can I do that

    Thanks in advance!!

    Jason



  • Mart1n0

    Hi,

    Thanks for the reply, I try the codes and it worked great, thanks again....by the way, why can't I delete files in "Temporary Internet Files" folder but I can in other folders What I have to do to be able to use the above codes to delete files in "Temporary Internet Files" folder

    Thanks in advance!!

    Jason



  • Delmonster

    Trying to delete a file which is in use will cause an IOException which you should catch. I don't think there's any way to find out whether a file is in use other than trying to do something to it and catching the exception.

  • Delete Access Denied!!