File Sharing on File.ReadAllText

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

    #1

    File Sharing on File.ReadAllText

    I'm doing:

    Dim Contents() As String = System.IO.File. ReadAllLines("C :\MyFile.txt") with
    upwards of 100,000 lines.
    This is a brilliantly simple and fast way of creating an array from a text
    file. However, it seems to be restricted as there are no overloads for
    FileSharing (I have another application which might add to the file whilst
    it is being read). The only alternative I have found is using a large amount
    of looping with StreamReader which takes forever.

    Is there another way?

    -Jerry






  • kimiraikkonen

    #2
    Re: File Sharing on File.ReadAllTex t

    On Sep 22, 10:02 am, "Jerry Spence1" <1...@dfgh.comw rote:
    I'm doing:
    >
    Dim Contents() As String = System.IO.File. ReadAllLines("C :\MyFile.txt")w ith
    upwards of 100,000 lines.
    This is a brilliantly simple and fast way of creating an array from a text
    file. However, it seems to be restricted as there are no overloads for
    FileSharing (I have another application which might add to the file whilst
    it is being read). The only alternative I have found is using a large amount
    of looping with StreamReader which takes forever.
    >
    Is there another way?
    >
    -Jerry
    Look at this thread to consider shared access explicty:



    HTH,

    Onur Güzel

    Comment

    • Chris Dunaway

      #3
      Re: File Sharing on File.ReadAllTex t

      On Sep 22, 2:02 am, "Jerry Spence1" <1...@dfgh.comw rote:
      I'm doing:
      >
      Dim Contents() As String = System.IO.File. ReadAllLines("C :\MyFile.txt") with
      upwards of 100,000 lines.
      This is a brilliantly simple and fast way of creating an array from a text
      file. However, it seems to be restricted as there are no overloads for
      FileSharing (I have another application which might add to the file whilst
      it is being read). The only alternative I have found is using a large amount
      of looping with StreamReader which takes forever.
      >
      Is there another way?
      >
      -Jerry
      I'm not sure why you think a StreamReader would any slower than
      calling ReadAllLines. Here is the code for the ReadAllLines method:

      Public Shared Function ReadAllLines(By Val path As String, ByVal
      encoding As Encoding) As String()
      Dim list As New ArrayList
      Using reader As StreamReader = New StreamReader(pa th, encoding)
      Dim str As String
      Do While (Not str = reader.ReadLine Is Nothing)
      list.Add(str)
      Loop
      End Using
      Return DirectCast(list .ToArray(GetTyp e(String)), String())
      End Function

      As you can see, it is using a StreamReader.

      Chris

      Comment

      • Jerry Spence1

        #4
        Re: File Sharing on File.ReadAllTex t


        "Chris Dunaway" <dunawayc@gmail .comwrote in message
        news:6037dcba-5f72-4d58-9062-50ac837168c5@m7 3g2000hsh.googl egroups.com...
        On Sep 22, 2:02 am, "Jerry Spence1" <1...@dfgh.comw rote:
        >I'm doing:
        >>
        >Dim Contents() As String = System.IO.File. ReadAllLines("C :\MyFile.txt")
        >with
        >upwards of 100,000 lines.
        >This is a brilliantly simple and fast way of creating an array from a
        >text
        >file. However, it seems to be restricted as there are no overloads for
        >FileSharing (I have another application which might add to the file
        >whilst
        >it is being read). The only alternative I have found is using a large
        >amount
        >of looping with StreamReader which takes forever.
        >>
        >Is there another way?
        >>
        >-Jerry
        >
        I'm not sure why you think a StreamReader would any slower than
        calling ReadAllLines. Here is the code for the ReadAllLines method:
        >
        Public Shared Function ReadAllLines(By Val path As String, ByVal
        encoding As Encoding) As String()
        Dim list As New ArrayList
        Using reader As StreamReader = New StreamReader(pa th, encoding)
        Dim str As String
        Do While (Not str = reader.ReadLine Is Nothing)
        list.Add(str)
        Loop
        End Using
        Return DirectCast(list .ToArray(GetTyp e(String)), String())
        End Function
        >
        As you can see, it is using a StreamReader.
        >
        Chris
        Thanks Chris. I believed it would be slower because you are looping round
        the Do While loop, and the System.File.Rea dAllLines happens in one line - no
        looping. However, I've just experimented with 1million lines and there
        doesn't seem to be that much difference, so I guess this is what
        System.File.Rea dAllLines does behind the scenes. However, I can't find a way
        of setting the FileShare using StreamReader which is what I need.

        -Jerry


        Comment

        • Chris Dunaway

          #5
          Re: File Sharing on File.ReadAllTex t

          On Sep 22, 5:27 pm, "Jerry Spence1" <1...@dfgh.comw rote:
          System.File.Rea dAllLines does behind the scenes. However, I can't find a way
          of setting the FileShare using StreamReader which is what I need.
          Just use a FileStream to open the file and use a streamreader to read
          it:

          Using fs As New FileStream("fil ename.txt", FileMode.Append ,
          FileAccess.Read Write, FileShare.ReadW rite)
          Using rdr As New StreamReader(fs )
          'User streamreader here...
          End Using
          End Using

          Chris

          Comment

          Working...