By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,641 Members | 1,446 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,641 IT Pros & Developers. It's quick & easy.

HELP - Check if a file is already open

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.