Check if a file is being used by another process

G

Guest

hello.

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

Cor Ligthert [MVP]

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
 
A

Andrew Morton

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
 
H

Herfried K. Wagner [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top