1

How do I write to an opened file in vb.net

I'm using this function right now

My.Computer.FileSystem.WriteAllText(filepath, createString(), True)

However, if the file is opened, it will give an error saying that the file is being used by another process. It works if the file is closed.

So how do i update the file content while it is open? The file need to be written while its open.

sicKo
  • 1,241
  • 1
  • 12
  • 35
  • You can only write to a file if you have opened it in the same process, using the _same_ variable used to open it. If the file is externally open, you can't write to it. Please explain where the file is open. – Oded Dec 06 '12 at 10:08
  • Example, user open the file by double clicking it. Then user try to update the file using an application in vb.net without closing the file first. So, you're saying there's no way the file can be updated if let say it's opened by external process? – sicKo Dec 07 '12 at 06:47
  • That's right. It is locked by the process that opened it. – Oded Dec 07 '12 at 09:59

1 Answers1

1

If the application reading the file is something you don't have the source code to there's not much you can do about it, it must be opening the file in a mode that denies write access. If you wrote the application that is reading the file you may be able to do something like the following to allow read/write sharing of the file:

Read file without exclusive lock

Community
  • 1
  • 1
PeterJ
  • 3,705
  • 28
  • 51
  • 71