Question file not free

andrews

Well-known member
Joined
Nov 22, 2011
Messages
167
Programming Experience
5-10
Hi,
It happens that a file is accessed by two applications for a little time ( < second) but then I get an error.
I could avoid that by seeing if the file is already used and then let the program wait for a little time (f.e. 1 second ) before trying again.
How to do ?
Something like this ?


dim good as boolean = false
do until good
' I have a sub for waiting some time(f.e. 1 second)
' now to see if the file is free then good = true (that is my question)
loop
' now accessing the file
Thanks for any resonse
 
Hi,

Have a look at this to see how you can handle identification of a file already in use:-

winforms - VB.NET Checking if a File is Open before proceeding with a Read/Write? - Stack Overflow

I personally would then NOT use a loop to keep checking to see if the file is free but instead I would add a Timer component set to one or two seconds to keep trying the code until the file becomes available. Doing things this way is then just a simple case of knowing when to enable and disable the timer.

Hope that helps.

Cheers,

Ian
 
Thanks Ian but :
<file.WriteLine("testing")>
When I can use the file I quess that I have the string "testing" into my file what I don't want.
 
Thanks Ian but :
<file.WriteLine("testing")>
When I can use the file I quess that I have the string "testing" into my file what I don't want.

Sorry andrews but I have no idea what that means.

Please take the time to expand your comments and explain fully what you mean. I will be back in an hour or so if no one else has helped before then.

Cheers,

Ian
 
Ian,
I mean that I want to know if the file is accessible without already writing something into it.
When the file is accessible I have already "testing" into it.
Is it not ?
 
Ian,
You gave me a link to a function
And I said :
I mean that I want to know if the file is accessible without already writing something into it.
When the file is accessible I have already "testing" into it, I do not want this.
Is it not ?
I have my own data to write.
What is wrong what I mean ?

<<<<<<<<<<<<<<<<<<<<<
Private holdPath As String = "defaultLog.txt"
Private _file As New FileStream(holdPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
Private file As New StreamWriter(_file)

Public Function Check(ByVal CheckNumber As Integer, ByVal CheckAmount As Decimal) As Decimal
Try
file.WriteLine("testing")
file.Close()
Catch e As IOException
'Note sure if this is the proper way.
End Try

Return 0D
End Function
>>>>>>>>>>>>>>>>>>>>>>>

 
Oh God, no wonder we are on different paths of understanding.

You have read and copied the Question posted by another user having a similar problem, which is wrong. You need to read the Answer below that Question with the BIG GREEN TICK next to it.

Once you have read that you should be able to get where you need to be.

Cheers,

Ian
 
Back
Top