Can VB.NET read file even if it is locked by another application?

Try this example.

1. Open a file in MS Word (Word locks the file).
2. Use 'Open With' and open the same file in Notepad (it works)
3. In VB.NET, I've tried File.ReadAllText, File.OpenRead and File.Open with read access and it says the file is locked.

If Notepad can open a file even if it is locked, surely there must be a way to do in VB.NET...

Does anyone know?
khoocAsked:
Who is Participating?
 
coopzzCommented:
yer I got caught on this also..

you need to allow the other application to write also to it so the share option is FileShare.ReadWrite.

Dim oFS As IO.FileStream = Nothing
Dim oFSt As IO.StreamReader = Nothing
oFS = New IO.FileStream(sPath & sLogFile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite)  <--- FileShare.ReadWrite.
oFSt = New IO.StreamReader(oFS)
Dim s As String = oFSt.ReadLine

CooPzZ
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.