borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Export Data From Crystal Report 8.5)Next Topic (How can I find out if a form is shown or hidden ?) New Topic New Poll Post Reply
AndreaVB Forum : VB General : How do I know if a text file is open and being written on?
Poster Message
matt_1ca
Level: Scholar

Registered: 27-03-2005
Posts: 42

icon How do I know if a text file is open and being written on?

I am wondering if there is a way to determine if a file is currently open and currently being written on?

Right now, the program I created is writting information on a user's file in the network.

Every user would have like 2 or 3 instances of the same program running which writes into the said user text file on certain set timer control interval.

Now there is no problem when this 2 or 3 instances are retained however with new workloads sometimes as much as 18 instances of the the said program has to be kept running simultaneously leading to a permission denied error when one instance of the program is writting on the said user file and another instance of the same program tries to write on the same user file as well.

I am thinking to myself if only there is a way for me to figure out if the file is currently being written on, then I can revise the program accordingly to avoid collisions which lead to the permission denied error I mentioned above..

All suggestions welcome and thank you so much for all the kind help you can give.

Gratefully,
Matt

18-05-2007 at 06:45 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: How do I know if a text file is open and being written on?

Hello. Regardless of the file type (some types, like .mdb, create a temporary file which you can search for), if you attempt to use a Name instruction on a file to rename it, you get a 75 error if the file is in use, no matter by what process.
So you can easily build a function to do the test, trying to rename the file: if you succeed, the file is not open.
Some kind of stuff like following:
Public Function FileInUse(FilePath As String) As Boolean
On Error GoTo ErrHandler
    Dim NewName As String
    ' assumes that the path actually points to an existant file
    NewName = FilePath & "***"
    ' try to rename the file
    Name FilePath As NewName
    ' if the code reaches this point, the file is not in use
    ' so we have to restore the previous name before exit
    Name NewName As FilePath
ExitPoint:
    Exit Function
ErrHandler:
    If Err.Number = 75
        FileInUse = True
    End If
    Resume ExitPoint
End Function
It doesn't manage cases like mistyped filenames or dir names, or else, but you can improve this technique by your own.
It's a trick: I found it by myself, and I use it when I need. But it's not documented, and the Name function is not easy to reach in the Help (if you type Name and press F1, you get addressed to the Name property)

Hope it helps.


____________________________
Real Programmer can count up to 1024 on his fingers

18-05-2007 at 10:33 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : How do I know if a text file is open and being written on?
Previous Topic (Export Data From Crystal Report 8.5)Next Topic (How can I find out if a form is shown or hidden ?) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder