OPEN A FILE FOR READ ONLY

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Morten Fagermoen

    #1

    OPEN A FILE FOR READ ONLY

    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.Fil eSystem.OpenTex tFileReader(arg uments(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



  • Chris Dunaway

    #2
    Re: OPEN A FILE FOR READ ONLY

    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.Fil eSystem.OpenTex tFileReader(arg uments(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.e xt", FileMode.Open,
    FileAccess.Read , FileShare.ReadW rite)

    Dim sr As New StreamReader(fs )

    'Use the stream reader here

    sr.Close()

    fs.Close()


    Hope this helps

    Comment

    • Morten Fagermoen

      #3
      Re: OPEN A FILE FOR READ ONLY

      Thank you, Chris!

      This tip made my day!!

      Morten


      "Chris Dunaway" <dunawayc@gmail .comwrote in message
      news:1160747141 .130126.233160@ b28g2000cwb.goo glegroups.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.Fil eSystem.OpenTex tFileReader(arg uments(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.e xt", FileMode.Open,
      FileAccess.Read , FileShare.ReadW rite)
      >
      Dim sr As New StreamReader(fs )
      >
      'Use the stream reader here
      >
      sr.Close()
      >
      fs.Close()
      >
      >
      Hope this helps
      >

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: OPEN A FILE FOR READ ONLY

        "Morten Fagermoen" <morten@fagermo en.comschrieb:
        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.Read Write' 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/>

        Comment

        Working...