By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,775 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,775 IT Pros & Developers. It's quick & easy.

Detecting if a File is in Use

P: n/a
ABN
I have a C# (.NET 1.1) application in which I loop over a number of files on
the hard drive and delete them. A few times, I've experienced an exception
that says the file is in use by another process.

So, my question is... Is there anyway to loop over the files and determine
if the files are able to be deleted before trying to delete (so the
exception won't be thrown)?

Thanks.


Apr 3 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a

"ABN" <no****@company.com> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
I have a C# (.NET 1.1) application in which I loop over a number of files
on
the hard drive and delete them. A few times, I've experienced an
exception
that says the file is in use by another process.

So, my question is... Is there anyway to loop over the files and
determine
if the files are able to be deleted before trying to delete (so the
exception won't be thrown)?


Is there a big advantage to doing this over simply catching the exception?
(And there's no guarantee that a file which wasn't in use when you checked
won't be in use when you then try to delete it, so you'd need to deal with
the exception anyway.)
Apr 3 '06 #2

P: n/a
Hello ABN,

You need to use WINAPI for this (interops call) to iterate through the processes
and find which one is holding your file handler.
But using exception is more natural way than P/Invoke

A> I have a C# (.NET 1.1) application in which I loop over a number of
A> files on the hard drive and delete them. A few times, I've
A> experienced an exception that says the file is in use by another
A> process.
A>
A> So, my question is... Is there anyway to loop over the files and
A> determine if the files are able to be deleted before trying to delete
A> (so the exception won't be thrown)?
A>
A> Thanks.
A>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Apr 4 '06 #3

P: n/a
Agreed.

Additionally, you have no guarantee that the user have the administrative
right to iterate through the processes.

It's not justifable to require admin. right to run the program solely for
this reason.

"Michael Nemtsev" <ne*****@msn.com>
???????:9c**************************@msnews.micros oft.com...
Hello ABN,

You need to use WINAPI for this (interops call) to iterate through the
processes and find which one is holding your file handler.
But using exception is more natural way than P/Invoke


Apr 4 '06 #4

P: n/a
Hi,

"Michael Nemtsev" <ne*****@msn.com> wrote in message
news:9c**************************@msnews.microsoft .com...
Hello ABN,

You need to use WINAPI for this (interops call) to iterate through the
processes and find which one is holding your file handler.
But using exception is more natural way than P/Invoke


And what happens if the first process that you checked opens it by the time
you reach the last process?

As Mike said, you have to deal with the exception no matter what. IMO there
is no way to know a priori if you can or cannot open a file except trying to
open it.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Apr 4 '06 #5

P: n/a
ABN,

Just catch the exception.
--
HTH
Stoitcho Goutsev (100)

"ABN" <no****@company.com> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
I have a C# (.NET 1.1) application in which I loop over a number of files
on
the hard drive and delete them. A few times, I've experienced an
exception
that says the file is in use by another process.

So, my question is... Is there anyway to loop over the files and
determine
if the files are able to be deleted before trying to delete (so the
exception won't be thrown)?

Thanks.

Apr 4 '06 #6

P: n/a
Michael,

Are you sure you can get this info using API?
--

Stoitcho Goutsev (100)

"Michael Nemtsev" <ne*****@msn.com> wrote in message
news:9c**************************@msnews.microsoft .com...
Hello ABN,

You need to use WINAPI for this (interops call) to iterate through the
processes and find which one is holding your file handler.
But using exception is more natural way than P/Invoke

A> I have a C# (.NET 1.1) application in which I loop over a number of
A> files on the hard drive and delete them. A few times, I've
A> experienced an exception that says the file is in use by another
A> process.
A> A> So, my question is... Is there anyway to loop over the files and
A> determine if the files are able to be deleted before trying to delete
A> (so the exception won't be thrown)?
A> A> Thanks.
A> ---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche

Apr 4 '06 #7

P: n/a
ABN
Thanks all for your responses. I have been and am still catching the
exception. I was hoping there would be some way to avoid this all together.

Anyway, let's say I have 100 files to delete, and file #34 is in use, throws
the exception, and I catch the exception. How can I skip this file and
continue deleting file #35 and on?

"ABN" <no****@company.com> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
I have a C# (.NET 1.1) application in which I loop over a number of files on the hard drive and delete them. A few times, I've experienced an exception that says the file is in use by another process.

So, my question is... Is there anyway to loop over the files and determine if the files are able to be deleted before trying to delete (so the
exception won't be thrown)?

Thanks.

Apr 4 '06 #8

P: n/a
Catch the exception within the loop, and keep iterating.

foreach (<list of files>)
{
try
{
<delete the file>
}
catch(Exception ex)
{
<print warning that file cannot be deleted>
}
}
"ABN" <no****@company.com> wrote in message
news:uz**************@TK2MSFTNGP14.phx.gbl...
Thanks all for your responses. I have been and am still catching the
exception. I was hoping there would be some way to avoid this all
together.

Anyway, let's say I have 100 files to delete, and file #34 is in use,
throws
the exception, and I catch the exception. How can I skip this file and
continue deleting file #35 and on?

"ABN" <no****@company.com> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
I have a C# (.NET 1.1) application in which I loop over a number of files

on
the hard drive and delete them. A few times, I've experienced an

exception
that says the file is in use by another process.

So, my question is... Is there anyway to loop over the files and

determine
if the files are able to be deleted before trying to delete (so the
exception won't be thrown)?

Thanks.


Apr 4 '06 #9

P: n/a
Catching exception is possibly the most widespread bad habit out there.
How many times does one to state that catching System.Exception is bad to
stop people from advising to do so?

Always catch the specific exception, IOException in this case, or throw away
possible StackOverflow-, OutOfMemory- and ThreadAbort- Exception when
catching System.Exception. These three can be thrown at "any" time, so you
may end in trouble when thinking you just caught IOException...

"Mike Schilling" <ap@newsgroup.nospam> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...
Catch the exception within the loop, and keep iterating.

foreach (<list of files>)
{
try
{
<delete the file>
}
catch(Exception ex)
{
<print warning that file cannot be deleted>
}
}
"ABN" <no****@company.com> wrote in message
news:uz**************@TK2MSFTNGP14.phx.gbl...
Thanks all for your responses. I have been and am still catching the
exception. I was hoping there would be some way to avoid this all
together.

Anyway, let's say I have 100 files to delete, and file #34 is in use,
throws
the exception, and I catch the exception. How can I skip this file and
continue deleting file #35 and on?

"ABN" <no****@company.com> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
I have a C# (.NET 1.1) application in which I loop over a number of
files

on
the hard drive and delete them. A few times, I've experienced an

exception
that says the file is in use by another process.

So, my question is... Is there anyway to loop over the files and

determine
if the files are able to be deleted before trying to delete (so the
exception won't be thrown)?

Thanks.



Apr 4 '06 #10

P: n/a
You're right. I didn't take the time to look up which exceptions it makes
sense to catch, and it shows.

"Lebesgue" <no****@spam.jp> wrote in message
news:eQ**************@TK2MSFTNGP03.phx.gbl...
Catching exception is possibly the most widespread bad habit out there.
How many times does one to state that catching System.Exception is bad to
stop people from advising to do so?

Always catch the specific exception, IOException in this case, or throw
away possible StackOverflow-, OutOfMemory- and ThreadAbort- Exception when
catching System.Exception. These three can be thrown at "any" time, so you
may end in trouble when thinking you just caught IOException...

"Mike Schilling" <ap@newsgroup.nospam> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...
Catch the exception within the loop, and keep iterating.

foreach (<list of files>)
{
try
{
<delete the file>
}
catch(Exception ex)
{
<print warning that file cannot be deleted>
}
}
"ABN" <no****@company.com> wrote in message
news:uz**************@TK2MSFTNGP14.phx.gbl...
Thanks all for your responses. I have been and am still catching the
exception. I was hoping there would be some way to avoid this all
together.

Anyway, let's say I have 100 files to delete, and file #34 is in use,
throws
the exception, and I catch the exception. How can I skip this file and
continue deleting file #35 and on?

"ABN" <no****@company.com> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
I have a C# (.NET 1.1) application in which I loop over a number of
files
on
the hard drive and delete them. A few times, I've experienced an
exception
that says the file is in use by another process.

So, my question is... Is there anyway to loop over the files and
determine
if the files are able to be deleted before trying to delete (so the
exception won't be thrown)?

Thanks.




Apr 4 '06 #11

P: n/a
Hi,

"Lebesgue" <no****@spam.jp> wrote in message
news:eQ**************@TK2MSFTNGP03.phx.gbl...
Catching exception is possibly the most widespread bad habit out there.
How many times does one to state that catching System.Exception is bad to
stop people from advising to do so?


I'm of the opinion that the above rule is not written in stone, there are
cases (like the current) that I do not really care about the exception
itself, all I'm after is a flag indicating if the operation could be
performed or not. in these cases I do catch Exception and frankly it's much
clear and better that just catch the 8 different exceptions File.Delete can
throw.

I would definitely catch Exception in this case.
I'm ready to be flamed :)
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Apr 5 '06 #12

P: n/a
I agree, in this particular case (especially when the code is not intended
to run in production environment), it's feasible to catch System.Exception.
I just felt an urge to write down that this is bad.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2****************@TK2MSFTNGP02.phx.gbl...
Hi,

"Lebesgue" <no****@spam.jp> wrote in message
news:eQ**************@TK2MSFTNGP03.phx.gbl...
Catching exception is possibly the most widespread bad habit out there.
How many times does one to state that catching System.Exception is bad to
stop people from advising to do so?


I'm of the opinion that the above rule is not written in stone, there are
cases (like the current) that I do not really care about the exception
itself, all I'm after is a flag indicating if the operation could be
performed or not. in these cases I do catch Exception and frankly it's
much clear and better that just catch the 8 different exceptions
File.Delete can throw.

I would definitely catch Exception in this case.
I'm ready to be flamed :)
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Apr 5 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.