Justin G
Justin G

Reputation: 182

Decrypting file in VB.NET, process in use

I'm trying to decrypt a file in VB using this code:

Try
            fsInput = New System.IO.FileStream(strInputFile, FileMode.Open, FileAccess.Read)
            fsOutput = New System.IO.FileStream(strOutputFile, FileMode.OpenOrCreate, FileAccess.Write)
            fsOutput.SetLength(0)
            Dim bytBuffer(4096) As Byte
            Dim lngBytesProcessed As Long = 0
            Dim lngFileLength As Long = fsInput.Length
            Dim intBytesInCurrentBlock As Integer
            Dim csCryptoStream As CryptoStream
            Dim cspRijndael As New System.Security.Cryptography.RijndaelManaged
            Select Case Direction
                Case CryptoAction.ActionEncrypt
                    csCryptoStream = New CryptoStream(fsOutput, _
                    cspRijndael.CreateEncryptor(bytKey, bytIV), _
                    CryptoStreamMode.Write)
                Case CryptoAction.ActionDecrypt
                    csCryptoStream = New CryptoStream(fsOutput, _
                    cspRijndael.CreateDecryptor(bytKey, bytIV), _
                    CryptoStreamMode.Write)
            End Select
            While lngBytesProcessed < lngFileLength
                intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)
                csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
                lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock)
            End While
            csCryptoStream.Close()
        Catch ex As Exception
            Clipboard.SetText(ex.ToString)
        End Try
        fsInput.Close()
        fsOutput.Close()

Heres the error I'm getting now:

System.IO.IOException: The process cannot access the file 'C:\Users\suffICE\Desktop\asdas.icevault' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at IceVaultFileOpener.Cryptography.fileCrypt.EncryptOrDecryptFile(String strInputFile, String strOutputFile, Byte[] bytKey, Byte[] bytIV, CryptoAction Direction) in F:\IceVault Developer Directory\IceVaultFileOpener\IceVaultFileOpener\icevaultfile.vb:line 91

I've already checked if it's possible if the file is opened in any other program, but it is not. How can I fix this? Thanks.

Upvotes: 1

Views: 323

Answers (1)

LarryF
LarryF

Reputation: 5083

You need to add the fileshare parameter to your open statements. Something like a FileShare.Read option, on the output file. Without that parameter, they open in exclusive mode.

Also, put your .Close() statements after your End Try statement, so that they get closed no matter what happens. Also, make sure you AREN'T actually catching an error in this routine, and thus skipping the closing of the files, so that when you try and decrypt it, it's not already open by the SAME process. (Assuming you are attempting the Encrypt, and Decrypt in the same program, just for testing, etc...)

Upvotes: 1

Related Questions