1

I had searched a lot of examples, but none work perfect for me. I am using C#.

My application need to remove the files in folder, only when the file is closed.

The try-catch File.Open(...) method only works for certain filetype like doc, xls, ppt, pdf, mp3 etc, but not work for txt, zip, html etc...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
VHanded
  • 2,079
  • 4
  • 30
  • 55
  • possible duplicate of [C#: Is there a way to check if a file is in use?](http://stackoverflow.com/questions/876473/c-is-there-a-way-to-check-if-a-file-is-in-use) – Saeed Amiri Jan 04 '11 at 07:08

4 Answers4

11

The behavior your are seeing doesn't have anything to do with the file's extension or contents. It has to do with the way the associated applications treat those files. For example, Notepad, Internet Explorer, etc will not hold a lock on an opened file once the contents are read. That's why .txt and .html files are able to be opened.

Microsoft Office, virtually all media players, etc will hold a lock on the file. In the case of Office, it's doing so to make sure other programs don't delete/move the file out from under it. In the case of a media player, the files are usually too big to be read into memory completely. That's why those file types are locked when in use.

In other words, those files that appear to not be in use aren't actually in use. The program read the data from the file and close it and now it's done with it. There's really no easy way to determine whether or not another program has a particular file open if it no longer has an open handle to the file.

Josh
  • 68,005
  • 14
  • 144
  • 156
  • I understand the logic behind, thanks for your explanation. What I can do now is using timer to delete the file with the oldest 'Last Access Time'. – VHanded Jan 04 '11 at 14:16
  • Actually that won't work either on operating systems where the last access time is not enabled. Last access time can be disabled in Windows XP and is disabled by default on Vista and Windows 7. – Josh Jan 04 '11 at 14:32
0

Open the file in the binary mode File.Open(...) will work for all files.

Shashi
  • 2,860
  • 2
  • 34
  • 45
0

Try opening the file in write mode, I think there is something to specify that the lock is exculsive..but for some reason if your thread dies..dunno if that lock will be released automatically...

Mahesh
  • 1,583
  • 13
  • 18
0

all you need is to delete the file that is not in use ... rigth ... Simply ignore the exception thrown by File.Delete. Since it will not delete the file that is in use ..

try
{
    File.Delete(path);
}
catch(Exception e)
{
// ignore ... or whatever action
}

you can also catch specific exceptions to take specific action ... like IOException for file in use, UnauthorizedAccessException for read only files and permission issues etc ...

Checking file for opening and then trying to delete may still through exception as file may have been opened by some process between checking and deleting operations ..

Mubashir Khan
  • 1,464
  • 1
  • 12
  • 17
  • 1
    this will not work for certain application like notepad etc that is doesn't apply lock to the file. – VHanded Jan 04 '11 at 14:18