1

I got a service coded in c# whoes deleted somes web site files hosted on iis, before an update. But sometime when i delete the files, they stay there.

If I try to delete them manually, via explorer, the file are not deletable, because they are in state "Delete pending".

There is the way my service try to delete the file

 try
                {
                  // Enlève tout les attributs sur le fichiers afin de s'assurer que le fichier n'est pas en lecture seul
                  File.SetAttributes(file, FileAttributes.Normal);

                  // Supprime le fichier
                  File.Delete(file);
}

It's there a way to avoid this state ?

What can i do to force the delete by c# code?

Could i release all process to the file by c# code ?

The environnement is

IIS 7.5 Windows 2008-r2 .net 4.0

Thanks

4
  • Why close my question ? Nov 11, 2011 at 15:35
  • I think it will be probably moved on serverfault Nov 11, 2011 at 15:38
  • 1
    And now is it better ? Nov 11, 2011 at 15:46
  • 1
    It's could be nice people try to close the questions say why they wont see my question be close. Nov 11, 2011 at 15:48

2 Answers 2

1

You'll want to find out what other process has the files open. I don't know how you'd do that programatically, but speaking as a sysadmin, the handle.exe utility from SysInternals (or their Process Explorer GUI tool) can show that to you real-time.

Then you'll want to either:

  1. Avoid the condition that causes the file to be locked by the other process,
  2. Close the handle if you can do so safely.
2
  • 1
    Thanks for you answer but the question was how to do it programatically. I really do not understand why my question was migrated to server fault. Nov 11, 2011 at 16:50
  • 1
    Too bad; I have no idea why the folks on SO would have voted to move it here.
    – mfinni
    Nov 11, 2011 at 19:23
0

You can get a list of files that are on that state through here I don't know how your files end up in that state but the first way that comes to my mind is to rename and then delete the file there is a similar solution for C++ on SO here

In C# it should be something like

File.Move(oldFileName, newFileName);
File.Delete(newFileName);
2
  • You think the move will unlock the file ? Nov 16, 2011 at 13:25
  • It renames the file.
    – user
    Nov 16, 2011 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .