StreamReader (ReadOnly)

  • Thread starter Pantelis Sotiropoulos
  • Start date
P

Pantelis Sotiropoulos

Greetings!

I'm opening a text file with as a StreamReader. The problem is when the file
is used by another user or Windows, it won't open! When I double click the
file it opens it. That's strange. Shouldn't it open it as read-only? Is
there an option to force it to open the file?

I'm using:
Dim sr As New StreamReader("C:\myfile.txt")

what if I use:
Dim s As New Stream = File.OpenRead("C:\myfile.txt")
Dim sr As New StreamReader(s)

would that work??
My final option is copying the file to a temp directory and open it from
there.
 
S

scorpion53061

Answer provided by Herfried Wagner....

* "Cor Ligthert said:
Did you send this with a reason to the language VB group or by accident?

However a file using the streamreader/writer will be locked until it is
closed.


Not necessarily:

\\\
Dim fs1 As New System.IO.FileStream("C:\Log.txt", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite)
Dim fs2 As New System.IO.FileStream("C:\Log.txt", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite)
Dim sr1 As New System.IO.StreamReader(fs1)
Dim sr2 As New System.IO.StreamReader(fs2)
MsgBox(sr1.ReadLine())
MsgBox(sr2.ReadLine())
sr1.Close()
sr2.Close()
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top