Question Read Only

solfinker

Well-known member
Joined
Aug 6, 2013
Messages
71
Programming Experience
Beginner
I have problems reading a file to process it as it is blocked by another application. Is there a way to open it "read only"?
I' m using:

Imports System.IO
If System.IO.File.Exists(fName) = True Then
Dim tmpstream As StreamReader = File.OpenText(fName)
TextLine = tmpstream.ReadToEnd().Split(Environment.NewLine)
...

tmpstream.Close() 'close reader
Else
MsgBox("File doesn't exist")
End If

Thanks a lot for your help
 
OpenText already opens the file for reading only. It will be allowed if the process that opened the file first said this was ok. Normally if a process has a file open for writing it will not allow subsequent processes to open it also, even for reading, because the file may not be in a readable state. In .Net FileShare enumeration controls this.
 
Thank you very much for your answer.
The file in question is opened with the notebook and even with MS-Excel read-only mode. The message MS-Excel gives when trying to open it is "filexxx is locked for editing by 'another user'. Open 'Read-Only' or click..."
But when trying to process it with my little vb.net application, it fails.:apologetic:
The message - you already know - is: Unhandled exception... The process cannot access the file 'C\Users\.... because it is being used by another process."
 
I'm thinking now about copying and renaming the file and trying to process the new file instead.
I hope it works, eventhough it may happen that I am not allowed to copy it either.
 
Back
Top