473,385 Members | 1,798 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,385 software developers and data experts.

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 1442
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
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
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
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
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
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
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
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
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
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
by: sherifffruitfly | last post by:
Try to do something with it, and look at the exception? Some other way? Thanks!
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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...

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.