• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1523
  • Last Modified:

Notepad can open it, but StreamReader gives me a "file in use" error

I've been working on an application in VB.Net that will read the logfile created by Windows Update.   This logfile is typically located at C:\WINDOWS\SoftwareDistribution\ReportingEvents.log

However, my program fails with the following message:
    The process cannot access the file 'C:\WINDOWS\SoftwareDistribution\ReportingEvents.log' because it is being used by another process.

But I can open this file sucessfully with Notepad and every program that I can think of *EXCEPT* from applications written with the .Net Framework.   So, what's going on...   why can Notepad/every-other-program-on-the-planet open it, and my app can't?

        Dim sr As StreamReader
        Dim buf, parts(), Backup, msg As String
        Dim col As Integer
        Const Path As String = "C:\WINDOWS\SoftwareDistribution\ReportingEvents.log"

        Try

            msg = ""
            sr = New StreamReader(Path, True)
            Do While Not sr.Peek < 0
                buf = sr.ReadLine()
                parts = buf.Split(vbTab)
                If parts.Length = 12 Then
                    col = 3
                Else
                    col = 4
                End If
                If parts(col + 2) <> "{00000000-0000-0000-0000-000000000000}" Then
                    msg = parts(col + 5) & ", " & parts(col + 6) & ", " & parts(col + 7)
                End If
                'Debug.WriteLine("Status Code: " & parts(col) & ", GUID: " & parts(col + 2) & ", Method:" & parts(col + 5) & ", Results:" & parts(col + 6) & ", Status:" & parts(col + 7))
            Loop
            MsgBox(msg)
            sr.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try
0
graye
Asked:
graye
  • 2
1 Solution
 
Fernando SotoCommented:
Hi graye;

Make the following changes to your code

    ' Add this line of code
    Dim PathStream As Stream = File.Open(Path, FileMode.Open, _
        FileAccess.Read, FileShare.ReadWrite)
    ' Modify this line with the stream name above
    sr = New StreamReader(PathStream, True)

StreamReader I believe tries to open the file exclusive.

Fernando
0
 
grayeAuthor Commented:
Thanks, that worked.

Oddly enough, the documentation says that the FileShare parameter to the Open method is for locking *other* threads...
0
 
Fernando SotoCommented:
Glad I was able to help. :=)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now