473,587 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(fi lename);
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 13353
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(fi lename);
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******@newsg roup.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
9087
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 data, imports it into a mySQL database and deletes the .csv-file after the import. so far, so good. but in some cases the cronjobs starts...
3
3671
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 instance and have to instances of the same program running togheter... Can someone help me on this? Fabio P.
11
3745
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 2003 Server or ADO or ODBC issue, I am posting this on all of the three newsgroups. That's the setup: Windows 2003 Server with IIS and ASP.NET...
1
2091
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 see if this document is already opened and if so, close it before i attempted to overwrite it. a code snippet would be helpful. my code as it...
6
55965
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 check if a file is open before deleting it Something like this
9
4312
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 = fileExists(filePath) // return 0, 1, or 2 2 means file exists and it si open but VB returns boolean value telling file exist or not
4
3005
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 actions if the file is already opened in another instance of visio. how do i check if it's already opened and then throw an error msg? thanks
7
4218
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 and maybe a list of thread which are accessing that file. The best way would be to have an event rising when the file is closed but a simple "every...
6
34633
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 I would ignore processing that particular file and move to next file. How can I check whether the file is open or not? I tried os.stat and...
0
8215
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. ...
0
8220
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...
0
6626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5394
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
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
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
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...

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.