OPEN A FILE FOR READ ONLY

  • Thread starter Thread starter Morten Fagermoen
  • Start date Start date
M

Morten Fagermoen

I want to open the current IIS log file in read-only mode. This file is held
by IIS but I still can open it in Notepad, but I have not been able (so far)
to read from it using my VB.NET program.

I have tried:
Dim fileReader As StreamReader
Try
fileReader = My.Computer.FileSystem.OpenTextFileReader(arguments(1) &
"\ex" & LogDate & ".log")
Catch ex As .....

And this throws an exception when the file is already in use by IIS.

What can I do to make this work?

Regards

Morten Fagermoen
 
Morten said:
I want to open the current IIS log file in read-only mode. This file is held
by IIS but I still can open it in Notepad, but I have not been able (so far)
to read from it using my VB.NET program.

I have tried:
Dim fileReader As StreamReader
Try
fileReader = My.Computer.FileSystem.OpenTextFileReader(arguments(1) &
"\ex" & LogDate & ".log")
Catch ex As .....

And this throws an exception when the file is already in use by IIS.

What can I do to make this work?

You need to use a method that allows you to set the FileSharing mode.
Try a FileStream in connunction with a StreamReader:

Dim fs As New FileStream("c:\test\filename.ext", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite)

Dim sr As New StreamReader(fs)

'Use the stream reader here

sr.Close()

fs.Close()


Hope this helps
 
Morten Fagermoen said:
This tip made my day!!

Little explanation: The parameter of 'FileShare' does not specify which
rights your application requires but instead it specifies which rights other
applications are granted after your application opens the file. That's why
'FileShare.ReadWrite' is the specified value.
 
Back
Top