Differentiating FileLocked from other IOExceptions

M

marc.gibian

I am writing a small program in C#/.NET that reads input files as they
arrive from other processes. Therefore, it is perfectly reasonable for
a file to be busy while it is being deposited for use by my program.

FileStream inFile = null;
try {
inFile = new FileStream("myFilePath", FileMode.Open,
FileAccess.Read, FileShare.Non);
}
catch (IOException e) {
}

Does indeed catch a locked file exception.

My problem, though, is that it appears to also catch other errors. I
want to quietly proceed if I get a locked file exception, but record an
error to a log and skip the file when I get something other than a
locked file.
How do I differentiate between a locked file and other IOExceptions?
 
G

Guest

FileNotFound and a few other conditions subclass from IOException and you
can catch those cases by exception class type - but the file partially locked
and file in use cases are a pain because there is no specific
FilePartiallyLocked or FileInUse exception.

The only way that I know of is to trap the generic IOException and test the
Message property for the presence of appropriate subtext such as "cannot
access" - scary considering that the text message could change in a future
release of the framework...

--Richard

P.S. Don't quote me but I believe 'disk full', 'read only' and 'access
denied due to WIN32 ACL permissions' are reported as generic IOException's
too....
 
M

marc.gibian

Hi Richard,

I did think of doing the compare on the Message. It works today, but it
sure is a scary way to do this. I always hate it when a vendor decides
developers can handle too much detail and abstracts things like this.
Thanks for your reply.

-Marc
 

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