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

Posted on 2006-05-15
Last Modified: 2008-07-15
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
Question by:graye
    LVL 62

    Accepted Solution

    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.

    LVL 41

    Author Comment

    Thanks, that worked.

    Oddly enough, the documentation says that the FileShare parameter to the Open method is for locking *other* threads...
    LVL 62

    Expert Comment

    by:Fernando Soto
    Glad I was able to help. :=)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now