A file is being held open but it should be being closed. RRS feed

  • Question

  • I'm working on a visual studio extension that performs a post build action.  It opens the current projects output .dll file, then uploads it to a server.

    Now, to open the file and retrieve the contents I use:

    Dim filedata = File.ReadAllBytes(filepath)

    which should be closing the file.

    I've also tried:

        Private Function ReadAllFileBytes(filepath As String)
            Dim fileBytes As Byte() = Nothing
            Using fs As System.IO.FileStream = System.IO.File.Open(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite)
                Dim numBytesToRead As Integer = Convert.ToInt32(fs.Length)
                fileBytes = New Byte((numBytesToRead) - 1) {}
                fs.Read(fileBytes, 0, numBytesToRead)
            End Using
            Return fileBytes
        End Function

    Which I'v read that a shared read write access may solve the problem but to no avail.  Neither method worked and it's giving me:

    Warning 1 Unable to delete file "C:\Users\taylor\Documents\Visual Studio 2012\Projects\prtg-beta\bin\Debug\stackadvisors.projects.prtg_beta.dll". Access to the path 'C:\Users\taylor\Documents\Visual Studio 2012\Projects\****-beta\bin\Debug\*********.dll' is denied.

    So it's like Visual Studio is keeping it open.  Even if my extension is being held up, neither of those functions should be keeping the file open.  They are not failing out or hitting exceptions. Also this is the only place in the extension I use GetFileBytes(), I did a search.

    It runs fine once, so I know the file is free when the build is done but when I run again, it seems to lock the file.  

    My question is why would using these two methods to read a file keep the file open?  Or is visual studio keeping the output file open?  Any insights would be great!


    Taylor D.

    Taylor Deiaco Developer

    Thursday, July 14, 2016 5:36 PM


All replies