Home All Groups Group Topic Archive Search About

OPEN A FILE FOR READ ONLY

Author
13 Oct 2006 12:17 PM
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

Author
13 Oct 2006 1:45 PM
Chris Dunaway
Morten Fagermoen wrote:
Show quoteHide quote
> 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
Author
13 Oct 2006 6:07 PM
Morten Fagermoen
Thank you, Chris!

This tip made my day!!

Morten


Show quoteHide quote
"Chris Dunaway" <dunaw***@gmail.com> wrote in message
news:1160747141.130126.233160@b28g2000cwb.googlegroups.com...
> Morten Fagermoen wrote:
>> 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
>
Author
13 Oct 2006 11:57 PM
Herfried K. Wagner [MVP]
"Morten Fagermoen" <mor***@fagermoen.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>