• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1527
  • 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"


            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
                    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))
        Catch ex As Exception
            Exit Sub
        End Try
  • 2
1 Solution
Fernando SotoRetiredCommented:
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.

grayeAuthor Commented:
Thanks, that worked.

Oddly enough, the documentation says that the FileShare parameter to the Open method is for locking *other* threads...
Fernando SotoRetiredCommented:
Glad I was able to help. :=)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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