StreamReader locks the file

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

    StreamReader locks the file

    I am using VB.NET 2008 and StreamReader to read from a file on our server.
    While the file is open in this VB.NET program, another program written in
    VB6 at the same time trying to append to the same file, and it got
    "Permission Denied" error.
    I am wondering if I can use StreamReader without locking the file for update
    by other program.

    This is how I open the file
    srBCAS = New StreamReader(m_ sServer & "\log.txt",
    System.Text.Enc oding.Default)

    Thank you


  • Herfried K. Wagner [MVP]

    #2
    Re: StreamReader locks the file

    "fniles" <fniles@pfmail. comschrieb:
    >I am using VB.NET 2008 and StreamReader to read from a file on our server.
    While the file is open in this VB.NET program, another program written in
    VB6 at the same time trying to append to the same file, and it got
    "Permission Denied" error.
    I am wondering if I can use StreamReader without locking the file for
    update by other program.
    You have to allow shared access explicitly:

    \\\
    Dim Stream As New FileStream( _
    "C:\foo.txt ", _
    FileMode.Open, _
    FileAccess.Read , _
    FileShare.ReadW rite _
    )
    Using Reader As New StreamReader(St ream)
    ...
    End Using
    ///

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

    Comment

    • kimiraikkonen

      #3
      Re: StreamReader locks the file

      On Jun 16, 8:35 pm, "fniles" <fni...@pfmail. comwrote:
      I am using VB.NET 2008 and StreamReader to read from a file on our server.
      While the file is open in this VB.NET program, another program written in
      VB6 at the same time trying to append to the same file, and it got
      "Permission Denied" error.
      I am wondering if I can use StreamReader without locking the file for update
      by other program.
      >
      This is how I open the file
      srBCAS = New StreamReader(m_ sServer & "\log.txt",
      System.Text.Enc oding.Default)
      >
      Thank you
      I beleive you'd better use "Using" statement to do all the disposal
      operation automatically, thus the underlying and all resources will be
      freed.

      Using srBCAS As New StreamReader(m_ sServer & "\log.txt",
      System.Text.Enc oding.Default)

      ' Code...Typicall y srBACS.ReadToEn d()

      End Using

      Hope this helps,

      Onur Güzel

      Comment

      • =?ISO-8859-1?Q?G=F6ran_Andersson?=

        #4
        Re: StreamReader locks the file

        kimiraikkonen wrote:
        On Jun 16, 8:35 pm, "fniles" <fni...@pfmail. comwrote:
        >I am using VB.NET 2008 and StreamReader to read from a file on our server.
        >While the file is open in this VB.NET program, another program written in
        >VB6 at the same time trying to append to the same file, and it got
        >"Permission Denied" error.
        >I am wondering if I can use StreamReader without locking the file for update
        >by other program.
        >>
        >This is how I open the file
        >srBCAS = New StreamReader(m_ sServer & "\log.txt",
        >System.Text.En coding.Default)
        >>
        >Thank you
        >
        I beleive you'd better use "Using" statement to do all the disposal
        operation automatically, thus the underlying and all resources will be
        freed.
        >
        Using srBCAS As New StreamReader(m_ sServer & "\log.txt",
        System.Text.Enc oding.Default)
        >
        ' Code...Typicall y srBACS.ReadToEn d()
        >
        End Using
        >
        Hope this helps,
        >
        Onur Güzel
        That is helpful for making sure that the file is properly closed when
        you are done with it.

        However, that is not the main problem here. The OP want's one process to
        be able to write to the file while another process is still reading it.

        As already mentioned, the solution is to specify the sharing mode when
        opening the stream.

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • fniles

          #5
          Re: StreamReader locks the file

          Thank you. That fixed my problem.
          Thanks again

          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
          news:Oe7NKY$zIH A.5832@TK2MSFTN GP02.phx.gbl...
          "fniles" <fniles@pfmail. comschrieb:
          >>I am using VB.NET 2008 and StreamReader to read from a file on our server.
          >While the file is open in this VB.NET program, another program written in
          >VB6 at the same time trying to append to the same file, and it got
          >"Permission Denied" error.
          >I am wondering if I can use StreamReader without locking the file for
          >update by other program.
          >
          You have to allow shared access explicitly:
          >
          \\\
          Dim Stream As New FileStream( _
          "C:\foo.txt ", _
          FileMode.Open, _
          FileAccess.Read , _
          FileShare.ReadW rite _
          )
          Using Reader As New StreamReader(St ream)
          ...
          End Using
          ///
          >
          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          Working...