473,749 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ 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(fil ePath)

but what happens if I am trying to delete the file that is open???

In java you would do someting like
int status = fileExists(file Path) // 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(fil ePath)

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

_dino_
Nov 21 '05 #1
9 4321
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(fil ePath)

but what happens if I am trying to delete the file that is open???

In java you would do someting like
int status = fileExists(file Path) // 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(fil ePath)

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(myF ile) then
If file is open ' how to check this
close it ' how to do this
end if

File.Delete(myF ile)
end if

Thanks for your help

_dino_

On Thu, 22 Sep 2005 18:51:01 -0700, Dennis
<De****@discuss ions.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(myF ile) then
If file is open ' how to check this
close it ' how to do this
end if

File.Delete(myF ile)
end if

Thanks for your help

_dino_

On Thu, 22 Sep 2005 18:51:01 -0700, Dennis
<De****@discuss ions.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****@discuss ions.microsoft. com> wrote in message
news:FD******** *************** ***********@mic rosoft.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(myF ile) then
If file is open ' how to check this
close it ' how to do this
end if

File.Delete(myF ile)
end if

Thanks for your help

_dino_

On Thu, 22 Sep 2005 18:51:01 -0700, Dennis
<De****@discuss ions.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****@discuss ions.microsoft. com> wrote in message
news:FD******** *************** ***********@mic rosoft.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(myF ile) then
If file is open ' how to check this
close it ' how to do this
end if

File.Delete(myF ile)
end if

Thanks for your help

_dino_

On Thu, 22 Sep 2005 18:51:01 -0700, Dennis
<De****@discuss ions.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(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

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********@gma il.com> wrote:
Hi Dino,

Try this function

Public Shared Function IsFileOpen(ByVa l filename As String) As
Boolean
Try
System.IO.File. Open(filename, IO.FileMode.Ope n,
IO.FileAccess. Read, IO.FileShare.No ne)
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********@gma il.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
3929
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 them a caption, storing the caption and filename in a text file. It's a bit buggy when removing the photos and captions from the file, and also in displaying them on the delete page. you can see it in action at www.4am.com.au/gallery/upload.php...
1
2493
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 the institution databank to avoid frauds on my system. The idea is to build a "new user register page" and with a CPF field. So, when the person enter his CPF, the ASP page would check on the institution databank and validate or not the...
4
2759
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 hope that someone could help me out. As said in an earlier post I am new to c++, this is alot harder to do than VB. I am for the most part self taught with what I already know and looking to learn more. The book I am using to teach myself from...
3
5572
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 cannot find it anywhere... Any clues... Already went into the help file and nothing... Also try to copy my database and the same message comes up
0
5573
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
4
2215
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 wanted to move my work and needed to place it on the server. Using VS 2005 , went to BUILD -Publish Web Site Checked the box for :: Alow this precompiled site to be updatable.
6
34649
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 I would ignore processing that particular file and move to next file. How can I check whether the file is open or not? I tried os.stat and os.access but I am not getting the expected results. Also I checked in IO exceptions, IO error handler...
2
13408
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); sw.Close(); } catch (IOException) {
4
2158
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 my fpga design work. I tried rectifying the errors that occurred - added the semicolon in line 459 , removed the brackets in line 460 and aligned the 'line' word in line 274 . This removed the compiler errors but I'm unsure of what exactly to do...
6
2876
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();
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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
9566
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
9333
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
9254
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
8256
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...
0
4608
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.