473,503 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a standard way to check whether or not a file is in use?

Try to do something with it, and look at the exception? Some other
way?

Thanks!
Apr 11 '08 #1
3 1444
On Fri, 11 Apr 2008 10:48:49 -0700, sherifffruitfly
<sh*************@gmail.comwrote:
Try to do something with it, and look at the exception? Some other
way?
It depends on why you want to know. If you're trying to find out whether
it's in use because you yourself want to use it, then yes...the best,
"standard" way is to simply attempt the operation and see if that
operation succeeds.

If you want to simply inspect the state without affecting the file, I
don't think that's possible from within .NET. However, there are
lower-level OS mechanisms that you could use. This isn't an appropriate
forum for learning about them though. You'd be better off in a newsgroup
specific to unmanaged Win32 programming.

Pete
Apr 11 '08 #2
On Apr 11, 11:04 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 11 Apr 2008 10:48:49 -0700, sherifffruitfly

<sherifffruit...@gmail.comwrote:
Try to do something with it, and look at the exception? Some other
way?

It depends on why you want to know. If you're trying to find out whether
it's in use because you yourself want to use it, then yes...the best,
"standard" way is to simply attempt the operation and see if that
operation succeeds.
Good enough - I just typically shy away from "exception-based
programming", so I thought I'd see if there were another common way.
Thanks a bunch!
Apr 11 '08 #3
On Fri, 11 Apr 2008 11:16:49 -0700, sherifffruitfly
<sh*************@gmail.comwrote:
Good enough - I just typically shy away from "exception-based
programming", so I thought I'd see if there were another common way.
Not that I know of. Exceptions are expensive, granted. But so is file
i/o. :)

In any case, logically if you are going to need access to the file anyway,
it doesn't do you any good to check the file's status beforehand, since
the status could change between the time you check and the time you try to
use the file.

Even if the file's accessible, it might not be by the time you try to use
it. And even in the situation where checking first could theoretically
save you the cost of the exception, now you've got the potential for a
false negative; that is, the check could fail even though by the time you
try to use the file, you could have succeeded.

Pete
Apr 11 '08 #4

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

Similar topics

8
2934
by: Marko Faldix | last post by:
Hello, with Python 2.3 I can write umlauts (a,o,u umlaut) to a file with this piece of code: import codecs f = codecs.open("klotentest.txt", "w", "latin-1") print >>f, unicode("My umlauts...
2
1446
by: bruce varley | last post by:
Hello, I suspect a platform bug, but first I'd like to check whether the C++ standard library has changed the handling of streams in a way that creates the behaviour I'm observing. Hence this...
2
2405
by: Peng Yu | last post by:
Put #include <complex.h> in my source code. However, it seems that the included file isn't compatible with C99. I suspect that C99 GCC is not local machine. Is there any compiler tag to help me...
43
4911
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
31
2766
by: Bill Cunningham | last post by:
There must be some change in the standard because I ran into this error when I put this line in a header. # pragma once I only wanted the headers to be included once. The compiler said something...
7
33455
by: andylcx | last post by:
Hi all: The c++ language can check whether the file exist or not. I am wondering how c language does this job? Thanks a lot! Andy
5
4510
by: funkyj | last post by:
I love pexpect because it means I may never have to use expect again (I don't do any heavy expect lifting -- I just need simple tty control)! As a python advocate I find it embarassing how...
270
9220
by: jacob navia | last post by:
In my "Happy Christmas" message, I proposed a function to read a file into a RAM buffer and return that buffer or NULL if the file doesn't exist or some other error is found. It is interesting...
126
6607
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
4
199
by: sherifffruitfly | last post by:
Try to do something with it, and look at the exception? Some other way? Thanks!
0
7204
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,...
0
7091
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...
0
7282
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,...
0
7342
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...
0
7464
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...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.