Check if a file is being used by another process

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hello.

how do you check if a file (say, a txt one) is being used by another process?
thanks.
 
Xero,

AFAIK is the only thing that you can do now is trying to read it in a try
and catch block.

This is in my opinion one of the omissions of the MS Dos and equals derived
operating systems.

I hope this helps,

Cor
 
Cor said:
Xero,

AFAIK is the only thing that you can do now is trying to read it in a
try and catch block.

I suppose you could iterate through all the processes on the machine and
find out which files they have handles to. This is the same as using
ProcessExplorer (www.sysinternals.com) and using it to search for a
filename.

Hmmm.... I can see System.Diagnostics.Process.HandleCount but not a way to
get the handles.

Andrew
 
Xero said:
how do you check if a file (say, a txt one) is being used by another
process?
thanks.

If you want to check if the file is exclusively locked, open a 'FileStream'
with 'FileShare.None' and 'FileAccess.ReadWrite', and catch the exception
which occurs if the file cannot be opened that way. Note that this will
only work if the application currently dealing with the file actually locked
the file.
 
Back
Top