473,493 Members | 4,333 Online
Bytes | Software Development & Data Engineering Community
Create 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 55933
I think this is the code you want
If File.Exists(path) Then
File.Delete(path)
End If

Curtis

"Dino Buljubasic" <di**@noplacelikehome.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 #2

No this will not work since if file exists it can still be open so
call to File.Delete(path) 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*****@hotmail.com>
wrote:
I think this is the code you want
If File.Exists(path) Then
File.Delete(path)
End If

Curtis

"Dino Buljubasic" <di**@noplacelikehome.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(ByVal filename As String) As Boolean
Try
System.IO.File.Open(filename, IO.FileMode.Open,
IO.FileAccess.Read, IO.FileShare.None)
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********@gmail.com> wrote:

¤ Hi,
¤
¤ you can try to open the file in exclusive mode.
¤
¤ Private Function IsFileOpen(ByVal filename As String) As Boolean
¤ Try
¤ System.IO.File.Open(filename, IO.FileMode.Open,
¤ IO.FileAccess.Read, IO.FileShare.None)
¤ 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***********************@swspectrum.com> wrote:
On 23 Sep 2005 01:33:01 -0700, "Filipe Marcelino" <fm********@gmail.com> wrote:

¤ Hi,
¤
¤ you can try to open the file in exclusive mode.
¤
¤ Private Function IsFileOpen(ByVal filename As String) As Boolean
¤ Try
¤ System.IO.File.Open(filename, IO.FileMode.Open,
¤ IO.FileAccess.Read, IO.FileShare.None)
¤ 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**@noplacelikehome.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
1620
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...
0
876
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...
2
4383
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...
1
1375
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...
3
3153
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...
1
4257
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...
1
2339
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...
5
5905
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...
5
1425
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...
2
4890
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...
0
7118
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
6980
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
7192
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...
1
6862
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...
0
5452
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,...
0
3087
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...
0
1397
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 ...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
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...

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.