|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
OPEN A FILE FOR READ ONLYI 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 Fagermoen wrote:
Show quoteHide quote > I want to open the current IIS log file in read-only mode. This file is held You need to use a method that allows you to set the FileSharing mode.> 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? 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 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 > "Morten Fagermoen" <mor***@fagermoen.com> schrieb: Little explanation: The parameter of 'FileShare' does not specify which > This tip made my day!! 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/> |
|||||||||||||||||||||||