473,466 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

HELP - Check if a file is already open

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

all I want to do is check if a file is open before calling
File.Delete(filePath)

I can not believe there is not easy way to check for this in VB ????

_dino_
Nov 21 '05 #1
9 4299
Try
File.Delete (filename)
catch
You can use variious catch statements to determine what the error is.
end try
--
Dennis in Houston
"Dino Buljubasic" wrote:
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

all I want to do is check if a file is open before calling
File.Delete(filePath)

I can not believe there is not easy way to check for this in VB ????

_dino_

Nov 21 '05 #2

yes, but how to close it if it was opened (say a pdf file)

I need to delete files from my temp directory. In order to do that I
have to check if the file I want to delete exists and if it is open
(because I can not call Delete on an open file)

if File.Exists(myFile) then
If file is open ' how to check this
close it ' how to do this
end if

File.Delete(myFile)
end if

Thanks for your help

_dino_

On Thu, 22 Sep 2005 18:51:01 -0700, Dennis
<De****@discussions.microsoft.com> wrote:
Try
File.Delete (filename)
catch
You can use variious catch statements to determine what the error is.
end try


Nov 21 '05 #3
If it's your application that opened it, then you should close it when you
are finished with it. If another application has the file open, it's not
polite to close it from your applicaiton as it could cause the other
application to throw an exception.
--
Dennis in Houston
"Dino Buljubasic" wrote:

yes, but how to close it if it was opened (say a pdf file)

I need to delete files from my temp directory. In order to do that I
have to check if the file I want to delete exists and if it is open
(because I can not call Delete on an open file)

if File.Exists(myFile) then
If file is open ' how to check this
close it ' how to do this
end if

File.Delete(myFile)
end if

Thanks for your help

_dino_

On Thu, 22 Sep 2005 18:51:01 -0700, Dennis
<De****@discussions.microsoft.com> wrote:
Try
File.Delete (filename)
catch
You can use variious catch statements to determine what the error is.
end try


Nov 21 '05 #4
God i hate answers like this Dennis. You didn't read his question properly
and so the first time you responded with the wrong answer and then this the
second time you proceed to lecture him on some supposed best practise.

He hasn't ask for a critique of his request but rather an answer to his
problem. If you mst include your preferences in any given scenario then at
least bundle them with an informed answer.

Ian

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:FD**********************************@microsof t.com...
If it's your application that opened it, then you should close it when you
are finished with it. If another application has the file open, it's not
polite to close it from your applicaiton as it could cause the other
application to throw an exception.
--
Dennis in Houston
"Dino Buljubasic" wrote:

yes, but how to close it if it was opened (say a pdf file)

I need to delete files from my temp directory. In order to do that I
have to check if the file I want to delete exists and if it is open
(because I can not call Delete on an open file)

if File.Exists(myFile) then
If file is open ' how to check this
close it ' how to do this
end if

File.Delete(myFile)
end if

Thanks for your help

_dino_

On Thu, 22 Sep 2005 18:51:01 -0700, Dennis
<De****@discussions.microsoft.com> wrote:
Try
File.Delete (filename)
catch
You can use variious catch statements to determine what the error is.
end try


Nov 21 '05 #5
An easy solution to your problem with my answers is to just not read them!
--
Dennis in Houston
"Ian Evitable" wrote:
God i hate answers like this Dennis. You didn't read his question properly
and so the first time you responded with the wrong answer and then this the
second time you proceed to lecture him on some supposed best practise.

He hasn't ask for a critique of his request but rather an answer to his
problem. If you mst include your preferences in any given scenario then at
least bundle them with an informed answer.

Ian

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:FD**********************************@microsof t.com...
If it's your application that opened it, then you should close it when you
are finished with it. If another application has the file open, it's not
polite to close it from your applicaiton as it could cause the other
application to throw an exception.
--
Dennis in Houston
"Dino Buljubasic" wrote:

yes, but how to close it if it was opened (say a pdf file)

I need to delete files from my temp directory. In order to do that I
have to check if the file I want to delete exists and if it is open
(because I can not call Delete on an open file)

if File.Exists(myFile) then
If file is open ' how to check this
close it ' how to do this
end if

File.Delete(myFile)
end if

Thanks for your help

_dino_

On Thu, 22 Sep 2005 18:51:01 -0700, Dennis
<De****@discussions.microsoft.com> wrote:

>Try
> File.Delete (filename)
>catch
> You can use variious catch statements to determine what the error is.
>end try


Nov 21 '05 #6
Hi Dino,

Try this function

Public Shared 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

This function tries to open the file in exclusive mode so, if the file
is in use, this function will return false else return true. But don't
try to do this with files that are opened with notepad or wordpad,
because they open the file and copies all the data to memory and then
they closes the file. So this function will not work with files opened
by such applications.

I hope it helps.

Regards,
Filipe Marcelino

Nov 21 '05 #7


I am asking for a way to check if a file called myFile is opened and
if it is to close it. The file can be of any extension such as .doc,
pdf, jpg, xls, bmp, txt, etc...

A simplest possible question, how to make my app clean its work, in
this case close all files that it has opened during its life cicle.
That is all.

I know that I can not call File.Delete on a file taht is open (e.g. a
pdf file is open), so I have to close it if it is open and then delete
it. The files are opened from my application by calling
Process.Start("...") - for a pdf that is Adobe

Thank you
_dino_
On 26 Sep 2005 01:11:57 -0700, "Filipe Marcelino"
<fm********@gmail.com> wrote:
Hi Dino,

Try this function

Public Shared 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

This function tries to open the file in exclusive mode so, if the file
is in use, this function will return false else return true. But don't
try to do this with files that are opened with notepad or wordpad,
because they open the file and copies all the data to memory and then
they closes the file. So this function will not work with files opened
by such applications.

I hope it helps.

Regards,
Filipe Marcelino


Nov 21 '05 #8
Hi Dino,

i think that's not possible because, since you open the files by
Process.Start(...) the application associated with the file you open is
in fact the one who really open the file, so your application will not
have any control about the file that Adobe Reader or any other app has
opened.

Regards,
Filipe Marcelino

Nov 21 '05 #9

OK I understand

I appreciate your help

On 26 Sep 2005 10:20:39 -0700, "Filipe Marcelino"
<fm********@gmail.com> wrote:
Hi Dino,

i think that's not possible because, since you open the files by
Process.Start(...) the application associated with the file you open is
in fact the one who really open the file, so your application will not
have any control about the file that Adobe Reader or any other app has
opened.

Regards,
Filipe Marcelino


Nov 21 '05 #10

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

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
1
by: Caliangelas | last post by:
Hello, I need a routine to check for a number called CPF (just like Social Security Number in USA). I already have a validation routine for that number, but I still need to check if it exists on...
4
by: kazack | last post by:
I posted a similiar question in this newsgroup already and got an answer which I already knew but didn't get the answer I was looking for so I am reposting the code and question differently in the...
3
by: Paolo | last post by:
Hi, I am trying to compact and repair my database, however every time I try it comes up a message saying: Table: "TempMSysAccessObject already exists", whenever I try to look for this table I...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
4
by: Brad Isaacs | last post by:
I am working with ASP.NET 2.0 and using an SQL Server 2000 database. I am using Visual Studio 2005 and developing on my Local machine. I am working with Login controls ASP.Configuration, I...
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...
2
by: Kimmo Laine | last post by:
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);...
4
by: rsaharia | last post by:
Hello All, I need help with this particular .pl file I picked up from http://www.veritools-usa.com/xnf2vhdl.htm What it's supposed to do is really convert an xnf file to a vhdl file. I need it for...
6
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
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
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
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
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.