473,608 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting if a File is in Use

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
12 1970

"ABN" <no****@company .com> wrote in message
news:eu******** ******@TK2MSFTN GP11.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
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
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.co m>
???????:9c***** *************** ******@msnews.m icrosoft.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
Hi,

"Michael Nemtsev" <ne*****@msn.co m> wrote in message
news:9c******** *************** ***@msnews.micr osoft.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
ABN,

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

"ABN" <no****@company .com> wrote in message
news:eu******** ******@TK2MSFTN GP11.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
Michael,

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

Stoitcho Goutsev (100)

"Michael Nemtsev" <ne*****@msn.co m> wrote in message
news:9c******** *************** ***@msnews.micr osoft.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
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******** ******@TK2MSFTN GP11.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
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******** ******@TK2MSFTN GP14.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******** ******@TK2MSFTN GP11.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
Catching exception is possibly the most widespread bad habit out there.
How many times does one to state that catching System.Exceptio n 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.Exceptio n. These three can be thrown at "any" time, so you
may end in trouble when thinking you just caught IOException...

"Mike Schilling" <ap@newsgroup.n ospam> wrote in message
news:uC******** ******@TK2MSFTN GP12.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******** ******@TK2MSFTN GP14.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******** ******@TK2MSFTN GP11.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

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

Similar topics

5
12540
by: Jole | last post by:
Hi I'm writing a program that needs to read from a file. In order for the program to be robust, it should somehow check that the file isn't corrupt, or stuffed in any way. For example, that file may have been created but a crash occurred at that point in time (while it was being created), damaging the file. Now, my program which needs to read from this file, should first check that it's in good condition, and that it hasn't been...
3
15669
by: Sagaert Johan | last post by:
I have a program that uses the filesystemwatcher. Problem is that i have to wait until the file is closed before i can do someting with it in the changed event. How can i detect if the foreighn application has finished writing and has closed the file ? Johan
2
3028
by: Sam-Kiwi | last post by:
I've spent the last 6 months developing a pay-per-download website using ASP.NET Users purchase documents and then download them. The intention is that users are only charged for documents they successfuly download. My problem revolves around detecting a successful download, the steps I take to handle the download are as follows:
7
1422
by: glen | last post by:
I'm looking for a way to detect if another process has a file open before a second process tries to open it. I've looked into the FileAttr function but I'm not sure if detecting a read-only state is sufficient. Can anyone confirm or deny? TIA...
3
1485
by: Nathan Sokalski | last post by:
I have several pieces of data that I use the HttpApplicationState for, because they rarely change and are used by everyone. When these pieces of data are created, they are created either from several files that rarely change or are a list of the files in a directory that I rarely add or remove files from. Therefore, I want to be able to detect when a file is added to, removed from, or modified in one of these directories. Because I create...
5
8748
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not work. I am sure that this probably fairly simple, but I have not been able to figure it out. Z.K.
7
16204
by: EliteBadger | last post by:
Hey, I've searched around on Google Groups for a while on this topic, and haven't found anything useful. I use a FileSystemWatcher to catch filesystem events. I would also like to get an event when a file is opened and when it is closed. It would also be nice if I could get more information about the process that owns the file--what its PID is, whether the file is opened for read sharing, rw sharing, etc. I know this sort of thing is...
1
1583
by: khandelwalk1 | last post by:
here is a java code, wich is supposed to read a file name from user n read its contents and display it, bt its nt working..ne1 dere cud help me...jus copy n paste dis code in a notepad file wid name TsetofFile.java n save another input notepad file in bin to give it as input to code. Execute this code n give dat file name...bt its showng File nt found..watz rong in dis...plz help me asap...!! import java.io.*; import java.net.*; class...
0
8025
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8509
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8493
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8365
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4053
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2493
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1620
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1363
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.