Results 1 to 3 of 3

Thread: Streamreader and shared file access

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    53

    Streamreader and shared file access

    Hi,

    Two different routines below doing two similar operations, I'm sure you get the 'jist'. I prefer using 'METHOD 1' as I'm going through my code trying to get away from anything that uses the visualbasic namespace.

    The file 'myfile.dat' is getting accessed by another program which is constantly being updated. If I try to use method 1 I get an error as another application is using it, which is true, Method 2 works fine because of the 'OpenShare.Shared', is there an equivalent for the streamreader.

    I just want to open using streamreader method a file that is being used.

    Anyone?

    Thanks
    Wayne


    Code:
    'METHOD 1
    Dim strBuffer As String
    Dim srFile As StreamReader = File.OpenText(Application.StartupPath & "\myfile.dat")
    strBuffer = srFile.ReadToEnd
    Dim InfoList() As String = strBuffer.Split(Environment.NewLine)
    srFile.Close()
    
    'METHOD 2
    Dim FileNum As Integer = FreeFile()
    FileOpen(FileNum, Application.StartupPath & "\myfile.dat", OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
    ProgValue = LineInput(FileNum)
    FileClose(FileNum)

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. 'METHOD 1
    2.         Dim strBuffer As String
    3.         Dim fs As New IO.FileStream(IO.Path.Combine(Application.StartupPath, "myfile.dat"), IO.FileMode.Open, IO.FileAccess.ReadWrite, IO.FileShare.ReadWrite)
    4.         Dim srFile As New IO.StreamReader(fs)
    5.         strBuffer = srFile.ReadToEnd
    6.         Dim InfoList() As String = strBuffer.Split(Environment.NewLine)
    7.         srFile.Close()

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    53
    Cheers..... That's what I was after.


    Thanks
    Wayne

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width