does StreamReader need exclusive access to the file?

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

    does StreamReader need exclusive access to the file?

    I'm using StreamReader to read a text file and sometimes I get an error
    saying that the file is opened by someone else.

    I haven't found any info about how to set a flag to tell StreamReader to
    open the file in shared mode and read mode.

    Thanks!
    Daniel



  • Miha Markic

    #2
    Re: does StreamReader need exclusive access to the file?

    Hi Daniel,

    Strema reader uses new FileStream(path , FileMode.Open, FileAccess.Read ,
    FileShare.Read) if you don't pass the already stream in the constructor.
    You might pass a valid stream object in the constructor of StreamReader if
    you feel like opening the file differently.
    And, don't forget to close it.
    --
    Miha Markic - RightHand .NET consulting & development
    miha at rthand com

    "Daniel P." <danutzp1@hotma il.comU> wrote in message
    news:uekQyjAwDH A.2048@TK2MSFTN GP10.phx.gbl...[color=blue]
    > I'm using StreamReader to read a text file and sometimes I get an error
    > saying that the file is opened by someone else.
    >
    > I haven't found any info about how to set a flag to tell StreamReader to
    > open the file in shared mode and read mode.
    >
    > Thanks!
    > Daniel
    >
    >
    >[/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: does StreamReader need exclusive access to the file?

      Daniel,

      In this case, you will want to use the FileStream object and open the
      file yourself with the appropriate access. Once you have that, you can pass
      the FileStream instance to the StreamReader constructor.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Daniel P." <danutzp1@hotma il.comU> wrote in message
      news:uekQyjAwDH A.2048@TK2MSFTN GP10.phx.gbl...[color=blue]
      > I'm using StreamReader to read a text file and sometimes I get an error
      > saying that the file is opened by someone else.
      >
      > I haven't found any info about how to set a flag to tell StreamReader to
      > open the file in shared mode and read mode.
      >
      > Thanks!
      > Daniel
      >
      >
      >[/color]


      Comment

      • Daniel P.

        #4
        Re: does StreamReader need exclusive access to the file?

        Thanks Nicholas!
        Thanks Miha!

        Do I close both the StreamReader and FileStream?

        If I use
        using( ... )
        {


        }

        do I still have to close it or Dispose will close the file automatically?

        Thanks!


        "Miha Markic" <miha at rthand com> wrote in message
        news:O4Wvy%23Aw DHA.2492@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        > Hi Daniel,
        >
        > Strema reader uses new FileStream(path , FileMode.Open, FileAccess.Read ,
        > FileShare.Read) if you don't pass the already stream in the constructor.
        > You might pass a valid stream object in the constructor of StreamReader if
        > you feel like opening the file differently.
        > And, don't forget to close it.
        > --
        > Miha Markic - RightHand .NET consulting & development
        > miha at rthand com
        >
        > "Daniel P." <danutzp1@hotma il.comU> wrote in message
        > news:uekQyjAwDH A.2048@TK2MSFTN GP10.phx.gbl...[color=green]
        > > I'm using StreamReader to read a text file and sometimes I get an error
        > > saying that the file is opened by someone else.
        > >
        > > I haven't found any info about how to set a flag to tell StreamReader to
        > > open the file in shared mode and read mode.
        > >
        > > Thanks!
        > > Daniel
        > >
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Miha Markic

          #5
          Re: does StreamReader need exclusive access to the file?

          Hi Daniel,

          I think that closing StreamReader is enough.
          Also, "using" on streamreader is enough.

          --
          Miha Markic - RightHand .NET consulting & development
          miha at rthand com

          "Daniel P." <danutzp1@hotma il.comU> wrote in message
          news:OWUz8bCwDH A.1872@TK2MSFTN GP09.phx.gbl...[color=blue]
          > Thanks Nicholas!
          > Thanks Miha!
          >
          > Do I close both the StreamReader and FileStream?
          >
          > If I use
          > using( ... )
          > {
          >
          >
          > }
          >
          > do I still have to close it or Dispose will close the file automatically?
          >
          > Thanks!
          >
          >
          > "Miha Markic" <miha at rthand com> wrote in message
          > news:O4Wvy%23Aw DHA.2492@TK2MSF TNGP12.phx.gbl. ..[color=green]
          > > Hi Daniel,
          > >
          > > Strema reader uses new FileStream(path , FileMode.Open, FileAccess.Read ,
          > > FileShare.Read) if you don't pass the already stream in the constructor.
          > > You might pass a valid stream object in the constructor of StreamReader[/color][/color]
          if[color=blue][color=green]
          > > you feel like opening the file differently.
          > > And, don't forget to close it.
          > > --
          > > Miha Markic - RightHand .NET consulting & development
          > > miha at rthand com
          > >
          > > "Daniel P." <danutzp1@hotma il.comU> wrote in message
          > > news:uekQyjAwDH A.2048@TK2MSFTN GP10.phx.gbl...[color=darkred]
          > > > I'm using StreamReader to read a text file and sometimes I get an[/color][/color][/color]
          error[color=blue][color=green][color=darkred]
          > > > saying that the file is opened by someone else.
          > > >
          > > > I haven't found any info about how to set a flag to tell StreamReader[/color][/color][/color]
          to[color=blue][color=green][color=darkred]
          > > > open the file in shared mode and read mode.
          > > >
          > > > Thanks!
          > > > Daniel
          > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...