473,842 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check if a file is open

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

If File(fileName). IsOpen then
File(fileName). Close
end if

File(fileName). Delete

Thank you
_dino_
Nov 21 '05 #1
6 56006
I think this is the code you want
If File.Exists(pat h) Then
File.Delete(pat h)
End If

Curtis

"Dino Buljubasic" <di**@noplaceli kehome.com> wrote in message
news:fo******** *************** *********@4ax.c om...
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

If File(fileName). IsOpen then
File(fileName). Close
end if

File(fileName). Delete

Thank you
_dino_

Nov 21 '05 #2

No this will not work since if file exists it can still be open so
call to File.Delete(pat h) will crash the application

I need some way to check if the file is open before caling Delete

On Thu, 22 Sep 2005 16:49:15 -0500, "Curtis" <cs*****@hotmai l.com>
wrote:
I think this is the code you want
If File.Exists(pat h) Then
File.Delete(pat h)
End If

Curtis

"Dino Buljubasic" <di**@noplaceli kehome.com> wrote in message
news:fo******* *************** **********@4ax. com...
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

If File(fileName). IsOpen then
File(fileName). Close
end if

File(fileName). Delete

Thank you
_dino_


Nov 21 '05 #3
Hi,

you can try to open the file in exclusive mode.

Private Function IsFileOpen(ByVa l filename As String) As Boolean
Try
System.IO.File. Open(filename, IO.FileMode.Ope n,
IO.FileAccess.R ead, IO.FileShare.No ne)
FileClose(1)
Return False
Catch ex As Exception
Return True
End Try
End Function
Regards,
Filiep Marcelino

Nov 21 '05 #4
On 23 Sep 2005 01:33:01 -0700, "Filipe Marcelino" <fm********@gma il.com> wrote:

¤ Hi,
¤
¤ you can try to open the file in exclusive mode.
¤
¤ Private Function IsFileOpen(ByVa l filename As String) As Boolean
¤ Try
¤ System.IO.File. Open(filename, IO.FileMode.Ope n,
¤ IO.FileAccess.R ead, IO.FileShare.No ne)
¤ FileClose(1)
¤ Return False
¤ Catch ex As Exception
¤ Return True
¤ End Try
¤ End Function

Just make sure to check the Exception for the "file in use" error before returning True.
Paul
~~~~
Microsoft MVP (Visual Basic)
Nov 21 '05 #5
This does not really answer the question. The function checks for
file if it is open and returns true if file is open. My problem is
that I need to know how to close it if it was already open (say a pdf
file)

Here is the situation:

if a user wants to see a file, file data is fetched from db and the
file is created in a temp directory, then a process is run to open the
file (say a pdf, or jpg file)

When user terminates the application, the temp directory is cleaned of
all files that were opened during application life cycle.

PROBLEM: Say a user was viewing a file (say a pdf file in Adobe) and
forgot to close it. When user terminates the application, the
application will try to delete this file, but it can not because it is
opened and held by another process (in this case by Adobe).

How can I check if the file is opened and if it was, close it and then
delete the file. Otherwise if it was not opened, just delete it from
temp directory.

My application does this:

for each file in files
delete file
next

But I need something like this:

for each file in files
if file is open then
close file
end if

delete the file
next
I appreciate your help
_dino_

On Fri, 23 Sep 2005 12:44:56 -0500, Paul Clement
<Us************ ***********@sws pectrum.com> wrote:
On 23 Sep 2005 01:33:01 -0700, "Filipe Marcelino" <fm********@gma il.com> wrote:

¤ Hi,
¤
¤ you can try to open the file in exclusive mode.
¤
¤ Private Function IsFileOpen(ByVa l filename As String) As Boolean
¤ Try
¤ System.IO.File. Open(filename, IO.FileMode.Ope n,
¤ IO.FileAccess.R ead, IO.FileShare.No ne)
¤ FileClose(1)
¤ Return False
¤ Catch ex As Exception
¤ Return True
¤ End Try
¤ End Function

Just make sure to check the Exception for the "file in use" error before returning True.
Paul
~~~~
Microsoft MVP (Visual Basic)


Nov 21 '05 #6
On Fri, 23 Sep 2005 18:10:38 GMT, Dino Buljubasic <di**@noplaceli kehome.com> wrote:

¤ This does not really answer the question. The function checks for
¤ file if it is open and returns true if file is open. My problem is
¤ that I need to know how to close it if it was already open (say a pdf
¤ file)
¤
¤ Here is the situation:
¤
¤ if a user wants to see a file, file data is fetched from db and the
¤ file is created in a temp directory, then a process is run to open the
¤ file (say a pdf, or jpg file)
¤
¤ When user terminates the application, the temp directory is cleaned of
¤ all files that were opened during application life cycle.
¤
¤ PROBLEM: Say a user was viewing a file (say a pdf file in Adobe) and
¤ forgot to close it. When user terminates the application, the
¤ application will try to delete this file, but it can not because it is
¤ opened and held by another process (in this case by Adobe).
¤
¤ How can I check if the file is opened and if it was, close it and then
¤ delete the file. Otherwise if it was not opened, just delete it from
¤ temp directory.
¤

I don't think you're going to be able to close the file if another user has it open. Disconnecting a
user from an open file typically require administrative privileges anyway and may cause the user's
app to crash.
Paul
~~~~
Microsoft MVP (Visual Basic)
Nov 21 '05 #7

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

Similar topics

1
1639
by: Neil MacGaffey | last post by:
Hi - I'm new to python and am stymied by something that should be simple. The file open command below works fine on my desktop machine, but I cant get it to work on my laptop. Since using Python on my laptop is realistically the only way I'm going to find the time to learn Python, this is a big problem for me. Here's what I'm up against: PythonWin 2.3.3 (#51, Dec 18 2003, 20:22:39) on win32. Portions Copyright 1994-2001 Mark...
0
890
by: Flashman | last post by:
I know this is an odd question but is their some way for Visual C++ .NET 2003 to open a file with extension .URL without trying to open it as "shortcut". Use a third party cross-platform tool that has file(Text Files) that must have URL extension. Previously with Visual C++ 6.00 could simply open them. Now ..NET 2003 on file open: "Cannot resolve this link. This file may not be a shortcut" ???? Why try to open it as a short-cut! Just open...
2
4429
by: OutdoorGuy | last post by:
Greetings, I have a "newbie" question in relation to opening files from C#. I have a Windows form where I allow the user to type in a file extension in a text box (e.g., "xls"). I then take that extension and use that as my filter criteria for the File Open dialog. Once the user selects a file with that extension (from the File Open dialog), I simply want to open that file (whether it is an .xls file, .txt file, etc.). I am...
1
1400
by: ivan.svaljek | last post by:
I'm trying to create a procedure that will take a subset of data from sql server and store in an xml file. The problem is that the xml file is read on every session/page refresh, so there is a possibility for collision, since there is a function in global.asax that refreshes the xml file every x minutes. Is it possible to check the file open status from global.asax and get in queue for the next exclusive file open?
3
3202
by: Farshid Lashkari | last post by:
Hi, My goal is to detect all (or most) file dependencies of a script (i.e. modules, dlls, data files). Currently, after a script is finished executing I use sys.modules to determine all module dependencies, and I use win32process.EnumProcessModules to determine DLL dependencies. This works reasonably well, however I would still like to detect dependencies from regular file open calls. I did a little test by modifying __builtins__.open...
1
4278
by: jtertin | last post by:
I am currently using the following code to make sure a file is writable (i.e. is not in use) by using the CanWrite method of the FileStream object. Using the code below, the TextWriter is still trying to write to the file and is throwing an exception (File... in use by another process) whenever the output file (strFileName) is open. I am trying to prevent it from writing to it at all if this is the case... Any insight? Public...
1
2367
by: Dachshund Digital | last post by:
If Explorer can display a file path longer than 260 odd characters, why is it that System.IO.File.Open Method can not? Calling GetFiles from my.computer.fileystem can return paths longer but File.Open can not open them? For example the following path causes File.Open to freak. Could not find a part of the path 'E:\_Old Development\Volume I \_Visual Studio Projects (Old)\_Very Old Development\_Development Example(s)\How to improve...
5
5940
by: bvidinli | last post by:
is there a way to find out if file open in system ? - please write if you know a way other than lsof. because lsof if slow for me. i need a faster way. i deal with thousands of files... so, i need a faster / python way for this. thanks. -- Ý.Bahattin Vidinli Elk-Elektronik Müh.
5
1447
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I am able to upload files to a server with a .net web application. The problem is I am providing a hyperlink so the user can open the file after it is placed on the server. For the URL I am using the absolute path to the file and setting this dynamically after the file is uploaded. For the hyperlink I have the target set to _blank. When clicking on the hyperlink nothing happens, wondering if I need to use a file open control and if...
2
4914
by: kumarboston | last post by:
Hi All, How do we open certain number of files in for loop. In my case I have couple of files with name scd_1_2.dat here, the number 1 goes till 5 and 2 goes till 14, I am trying to open recursively but getting some error. #!/usr/bin/perl use strict; use warnings; for (my $i=1;$i<=5;$i++) {
0
9715
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
10940
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...
1
10670
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
10308
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
9451
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7854
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4499
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
2
4087
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3141
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.