0

I've developed a multi-user application. One user saves a string to a text file, other users have to read this string in a background process.

My problem is: when some users open the file to read the string, another user that wants to save a new string can't write to the file. I would like to implement a system that checks if the file is opened or not, and in case it is, waits a few seconds and retries.

What is the function or command to look if the file is already opened?

Michael
  • 57,169
  • 9
  • 80
  • 125
Pietro Z.
  • 521
  • 2
  • 6
  • 18

2 Answers2

1

I solved my problem using tips:

i've replaced this code:

         ip_acquisito = My.Computer.FileSystem.ReadAllText(path_elenco & "\ip.txt")

with this code:

     Using fs As FileStream = File.Open(path_elenco & "\ip.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
             Dim b(1024) As Byte
             Dim temp As UTF8Encoding = New UTF8Encoding(True)

             Do While fs.Read(b, 0, b.Length) > 0
                 ip_acquisito = temp.GetString(b)
             Loop
     End Using
Pietro Z.
  • 521
  • 2
  • 6
  • 18
0

There are quite a few patterns you can deal with. Two I'd recommend would be:

Community
  • 1
  • 1
Waescher
  • 5,361
  • 3
  • 34
  • 51