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

Is there a way to check if file is already open?

Hi!

Is there a way to check if file is already open/used by another process?

I know that i can do something like this to check it

try {
StreamWriter sw = new StreamWriter(filename);
sw.Close();
} catch (IOException) {
// File is already open...
}

but I would rather use some method then try to open the file in write mode.
thx
Kimmo
May 31 '07 #1
2 13269
Hi!
>
Is there a way to check if file is already open/used by another process?

I know that i can do something like this to check it

try {
StreamWriter sw = new StreamWriter(filename);
sw.Close();
} catch (IOException) {
// File is already open...
}

but I would rather use some method then try to open the file in write
mode.
It's transitory information which you shouldn't normally rely on. A file
that's opened/closed one moment could be closed/opened the next. The safest
route is to simply open the file with the appropriate "FileShare" access.
See this enumeration for details.
May 31 '07 #2
On Thu, 31 May 2007 07:30:12 -0700, Kimmo Laine <re******@newsgroup.only>
wrote:
Is there a way to check if file is already open/used by another process?

I know that i can do something like this to check it ["try/catch"
snipped]

but I would rather use some method then try to open the file in write
mode.
Why do you want this information?

Note that if you do not yourself open and lock the file, then checking
whether the file is open at any given moment in time is pointless. A
split second after you make the check, the state could change.

If you need to open the file yourself, then just try to open it. If you
don't need to open the file yourself, then there is not usually really any
use in knowing whether the file is already open or used by another process.

(One exception would be if you are trying to write a tool similar to the
Sysinternals FileMon utility, but the kind of things that tool does
requires much lower-level coding than .NET provides).

Pete
May 31 '07 #3

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

Similar topics

18
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the...
3
by: Fabio Pliger | last post by:
Hi, is it possibile, in python, to check for an already running instance of an application? My problem is that, if my program i running and the user relaunch it, i don't want to open a new...
11
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
1
by: Devhead | last post by:
i have an MS Word Document that i have created from an SQL server db and once created it is opened. If i try to create the same document with the same filename, i get an error. how do i check to...
6
by: Dino Buljubasic | last post by:
My application creates some temporary files that are deleted when my application terminates. However, if a temp file is open, it will not be deleted and application will crash. How can I...
9
by: Dino Buljubasic | last post by:
If I want to delete a file I can call File.Delete(filePath) but what happens if I am trying to delete the file that is open??? In java you would do someting like int status =...
4
by: jes | last post by:
hi, i have an open & delete btn. onclick of open as visio drawing opens in visio & onclick of delete the drawing gets deleted from the filesystem. The problem is i am unable to perform these...
7
by: Benjamin.von.Ardenne | last post by:
Hi guys, just to keep it short and simple - I'd like to "monitor" a file that is opend in Word e.g. and wait for it to be closed but I didn't find a way to check the current access state of a file...
6
by: Ros | last post by:
There are 10 files in the folder. I wish to process all the files one by one. But if the files are open or some processing is going on them then I do not want to disturb that process. In that case...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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:
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
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...

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.