File Access

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

All,

I need to know if anyone knows if there is a way to tell
if a file is currently in use by some process. I already
know you can wrap your file access call in a seperate try
block but I would think that there is a more direct way to
do that.

Ideally it would be part of the Framework but if there is
at least a direct API call I can make I would be happy
with that. Any information would be great. Thanks!
 
Thanks for the response. What you suggested is actually
what I am currently doing. While it works I would think
that there has to be a more direct way to get at that kind
of information. Obviously Windows must know what state a
file is in otherwise the System.IO.IOException could not
be raised when you try to access a file. I was hoping that
someone knows of a more direct way to get at that other
than the try/catch technique. I would think that there
would have to be some API call that could be done.

-----Original Message-----
That is a good question and I don't know the correct answer. But since no
one else has responded yet, here is one way that should work.

Simply try to open the file to write to and trap an error. Like this:

bool FileLocked = false;
try {System.IO.File.OpenWrite("MyFile.exe");}
catch (System.IO.IOException) {FileLocked = true;}


-Noah Coad
Microsoft MVP & MCP [.NET/C#]

Dan said:
All,

I need to know if anyone knows if there is a way to tell
if a file is currently in use by some process. I already
know you can wrap your file access call in a seperate try
block but I would think that there is a more direct way to
do that.

Ideally it would be part of the Framework but if there is
at least a direct API call I can make I would be happy
with that. Any information would be great. Thanks!


.
 
Back
Top