473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Differentiating FileLocked from other IOExceptions

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?

Nov 16 '05 #1
2 1482

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....

"ma*********@ACM.ORG" wrote:
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?

Nov 16 '05 #2
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

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: balg | last post by:
How can I validate whether a given file is a .Net Assembly / Com-ActiveX binary / Other ? Is it possible without opening the file twice, or even once ?
4
by: Peter Jaffe | last post by:
I've seen a few posts on this topic, but still haven't found a clean solution. Anyone have other suggestions or feel that one of the options I highlight below is the way to go? The Problem...
3
by: Suma | last post by:
Hi all, Is there any way from which we can find out if a window is opened as a modal window or as a normal window? I have a screen which opens as a normal window in one case and in other case...
8
by: Pietro | last post by:
Hi, i would like to know if there is a way to differentiate between requests, someting like a number that is unique only in the execution of an individual request. Thank you Pietro
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.