TedN Posted August 25, 2007 Posted August 25, 2007 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: 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. Quote
TedN Posted August 25, 2007 Author Posted August 25, 2007 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. Quote
MrPaul Posted August 25, 2007 Posted August 25, 2007 Using statement Version 2 of the framework introduced the Using construct which can be used to guarantee that resources are properly released: 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. :) Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.