Results 1 to 7 of 7

Thread: Reading File In Use by Another Process

  1. #1

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507

    Question Reading File In Use by Another Process

    Hi,

    I have a piece of code: -

    Code:
            Dim strTextLine As String
            Dim strCurrSystem As String
    
            Dim intFirstPos As Integer
            Dim intLastPos As Integer
            Dim intLength As Integer
    
            Dim strFiles() As String = System.IO.Directory.GetFiles(strLogPath, "netLog*")
    
            System.Array.Sort(strFiles, New FileInfoCompare)
    
            Dim strLines() As String = System.IO.File.ReadAllLines(strFiles(strFiles.Length - 1))
            Dim strLine As String
    
            Array.Reverse(strLines)
    
            For Each strLine In strLines
                strTextLine = strLine
                If strTextLine.Contains("System:") Then
                    intFirstPos = InStr(1, strTextLine, "(", CompareMethod.Text)
                    intLastPos = InStr(1, strTextLine, ")", CompareMethod.Text)
                    intLength = intLastPos - intFirstPos
                    strCurrSystem = Mid(strTextLine, intFirstPos + 1, intLength - 1)
                    Exit For
                End If
            Next
    Which reads a file being created by another application.

    The code runs fine when the other application isn't running, however when it is the file is in use which results in the above code throwing an error saying that it's in use by another process.

    Here: -

    Code:
    Dim strLines() As String = System.IO.File.ReadAllLines(strFiles(strFiles.Length - 1))
    So my question is how or what can I use to read the file whilst it's in use by another process?

    From the above code you can see that I read the file then reverse the array to carry out my required search. This I'd still like to do rather than read and search the complete file.

    Any thoughts or suggestions on how to avoid the error that gets thrown much appreciated.

    Thanks & Regards
    VB.Net (VS 2010)

  2. #2
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Reading File In Use by Another Process

    You can read the file, but only if the app writing it gives that permission.

    Assuming it does allow read access then you can do this (access of ReadWrite is necessary here, it's not a mistake);

    Code:
            Dim MyFile As String = "Stuff.txt"
            Dim CurrentLine As String
    
            'If file exists
            If System.IO.File.Exists(MyFile) Then
    
                ' Open a FileStream to read
                Using FS As New System.IO.FileStream(MyFile, _
                                                     System.IO.FileMode.Open, _
                                                     System.IO.FileAccess.Read, _
                                                     System.IO.FileShare.ReadWrite)
                    ' Open a StreamReader to read the FileStream
                    Using MyStreamReader As New System.IO.StreamReader(FS)
                        'While lines are available
                        While (MyStreamReader.Peek() > -1)
                            CurrentLine = MyStreamReader.ReadLine()
                            ' Do something with the line
                        End While
                        ' Close the StreamReader
                        MyStreamReader.Close()
                    End Using
                End Using
    
            End If
    If the other app is under your control, you can also write the file using the same shared access principle.
    Code:
            Using FS As New System.IO.FileStream(MyFile, _
                                                 System.IO.FileMode.Append, _
                                                 System.IO.FileAccess.Write, _
                                                 System.IO.FileShare.ReadWrite)
                Using MyStreamWriter As New System.IO.StreamWriter(FS)
                    MyStreamWriter.WriteLine("Something...")
                    MyStreamWriter.Close()
                End Using
            End Using
    Last edited by Bulldog; Mar 19th, 2015 at 12:09 PM.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  3. #3

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507

    Re: Reading File In Use by Another Process

    Hi,

    Many thanks for the reply.

    I presume that I build my Array to allow me to search backwards at the point: -

    ' Do something with the line
    Seeing as the StreamReader is reading a line one at a time how would I build the Array up?

    Thanks & Regards
    VB.Net (VS 2010)

  4. #4
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Reading File In Use by Another Process

    With a StreamReader you can read all of the text using .ReadToEnd()

    You can also do something like;
    Code:
            Dim lines As New List(Of String)()
            Dim i As New System.IO.StreamReader("a.txt")
            While (i.Peek > -1)
                lines.Add(i.ReadLine)
            End While
    That will give you a List of lines (starting from index 0 to lines.Count - 1).


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  5. #5

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507

    Re: Reading File In Use by Another Process

    Hi,

    I've made some changes to my code following your previous replies: -

    Code:
            Dim strTextLine As String
            Dim strCurrSystem As String
            Dim strLogFile As String
            Dim strLines As New List(Of String)()
    
            Dim intFirstPos As Integer
            Dim intLastPos As Integer
            Dim intLength As Integer
    
            Dim strFiles() As String = System.IO.Directory.GetFiles(strLogPath, "netLog*")
    
            System.Array.Sort(strFiles, New FileInfoCompare)
    
            Using FS As New System.IO.FileStream(strFiles(strFiles.Length - 1), IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite)
                Using MyStreamReader As New System.IO.StreamReader(FS)
                    While (MyStreamReader.Peek() > -1)
                        strLines.Add(MyStreamReader.ReadLine)
                    End While
                    MyStreamReader.Close()
                End Using
            End Using
    
            'Dim strLines() As String = System.IO.File.ReadAllLines(strFiles(strFiles.Length - 1))
            Dim strLine As String
    
            Array.Reverse(strLines)
    
            For Each strLine In strLines
                strTextLine = strLine
                If strTextLine.Contains("System:") Then
                    intFirstPos = InStr(1, strTextLine, "(", CompareMethod.Text)
                    intLastPos = InStr(1, strTextLine, ")", CompareMethod.Text)
                    intLength = intLastPos - intFirstPos
                    strCurrSystem = Mid(strTextLine, intFirstPos + 1, intLength - 1)
                    Exit For
                End If
            Next
    However, the following line throws an error when trying to run: -

    Code:
    Array.Reverse(strLines)
    Error: Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to 'System.Array'.

    I though this was an Array following the declaration at the start?

    Code:
    Dim strLines As New List(Of String)()
    Is this not the case?

    Thanks & Regards
    VB.Net (VS 2010)

  6. #6

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507

    Re: Reading File In Use by Another Process

    Hi,

    Please ignore the above......

    I found the problem and made the change: -

    Code:
    strLines.Reverse()
    Regards
    VB.Net (VS 2010)

  7. #7
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,390

    Re: Reading File In Use by Another Process

    list(of is a collection not an array. MSDN would of told you this.

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