Cannot Access File

TedN

Freshman
Joined
Sep 25, 2006
Messages
35
My program firsts reads a file then writes to the file. The program used to work fine on my old XP Home OS and using Visual Studio 2003. I've since changed to XP Pro and installed Visual Studio 2005.

I now get the following error:
"The process cannot access the file "C:\Haven.dat" because it is being used by another process."

It's got something to do with Haven.dat still being in use.


The relevant code is as follows:

Code:
Public Function Decrypt() As Integer

        Dim fsRead As FileStream
        Dim encStream As CryptoStream
        Dim sr As StreamReader

        fsRead = New FileStream("C:\Haven.dat", FileMode.Open, FileAccess.Read)
        encStream = New CryptoStream(fsRead, key.CreateDecryptor(), CryptoStreamMode.Read)
        sr = New StreamReader(encStream)

        fsRead.Close()
        encStream.Close()
        sr.Close()
        fsRead.Dispose()
        encStream.Dispose()
        sr.Dispose()

End Function


Public Function Encrypt() As Integer

        Dim fsWrite As New FileStream
        Dim encStream As CryptoStream        
        Dim sw As StreamWriter

        [B]fsWrite = New FileStream("C:\Haven.dat", FileMode.Create, FileAccess.Write)[/B]
        encStream = New CryptoStream(fsWrite, key.CreateEncryptor(), CryptoStreamMode.Write)
         sw = New System.IO.StreamWriter(encStream)
            
        fsWrite.Close()
        encStream.Close()
        sw.Close()
        fsWrite.Dispose()
        encStream.Dispose()
        sw.Dispose()
            
    End Function


The error occurs at the bolded line.
As a test I made a copy of the dat file and referenced the copy in the bolded line. There was then no error so it means that the original dat file is not being closed down.

Your help would be much appreciated.
 
OK. I found the problem. I downloaded and installed Unlocker and it showed the only process that was using the dat file was Visual Studio. So it was obviously my code. By a process of elimination Unlocker was able to show when the dat file became used.
And there it was, right at the beginning of the program where I opened a streamreader to check the password. I'd forgotten all about that - most embarrassing. I hadn't closed out the streamreader completely.


Strange the program worked OK in VS 2003.
 
Using statement

Version 2 of the framework introduced the Using construct which can be used to guarantee that resources are properly released:

Visual Basic:
Using fsRead = New FileStream("C:\Haven.dat", FileMode.Open, FileAccess.Read)
    Using encStream = New CryptoStream(fsRead, key.CreateDecryptor(), CryptoStreamMode.Read)
        Using sr = New StreamReader(encStream)

            'Use sr, encStream, and fsRead here

        End Using 'sr will be closed and disposed here
    End Using 'encStream will be closed and disposed here
End Using 'fsRead will be closed and disposed here

A Using block behaves like a Try...Finally construction in which the Try block uses the resources and the Finally block disposes of them. Because of this, the Using block guarantees disposal of the resources, no matter how you exit the block.

:)
 
Back
Top