HELP - Check if a file is already open

  • Thread starter Thread starter Dino Buljubasic
  • Start date Start date
D

Dino Buljubasic

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_
 
Try
File.Delete (filename)
catch
You can use variious catch statements to determine what the error is.
end try
 
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_
 
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.
 
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
 
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
 
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_
 
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
 
Back
Top