加密.txt文件错误-进程无法访问文件 [英] Encrypting .txt File Erroring - Process Cannot Access File
问题描述
我正在尝试加密在应用程序中创建的 .txt
文件.
I'm trying to encrypt a .txt
file which is created in my application.
要创建此文件,我使用以下代码:
To create this file, I'm using the following code:
Dim fileExists As Boolean = File.Exists(directorypath & "dbpw.txt")
If File.Exists(directorypath & "dbpw.txt") = False Then
Using sw As New StreamWriter(File.Open(directorypath & "dbpw.txt", FileMode.Create))
IIf(fileExists, "", "")
sw.Close()
End Using
End If
然后,要编写和加密文件,我使用以下代码,调用从Internet上的示例中调整的子例程.
Then, to write and encrypt the file, I'm using the following code, calling subroutines that I tweaked from examples on the internet.
bytKey = CreateKey(txtCode.Text)
bytIV = CreateIV(txtCode.Text)
EncryptOrDecryptFile(directorypath & "dbpw.txt", directorypath & "dbpw.txt", bytKey, bytIV, CryptoAction.ActionEncrypt)
当代码到达最后一行时,调用 EncryptOrDecrypt
子例程,则会引发错误提示
When the code gets to the final line, calling the EncryptOrDecrypt
subroutine, an error is thrown saying
该进程无法访问文件'myDirectoryPath \ dbpw.txt',因为该文件正在被另一个进程使用
The process cannot access the file 'myDirectoryPath\dbpw.txt' because it is being used by another process
我需要怎么做才能释放文件?
What do I need to do to release the file?
我也尝试只使用 File.Create
和 File.Encrypt
,但是无论哪种方式都抛出相同的错误.
I also tried just using File.Create
along with File.Encrypt
but the same error was thrown either way.
EncryptOrDecryptFile()
Public Sub EncryptOrDecryptFile(ByVal strInputFile As String, ByVal strOutputFile As String, ByVal bytKey() As Byte, ByVal bytIV() As Byte, ByVal Direction As CryptoAction)
Try
fsInput = New System.IO.FileStream(strInputFile, FileMode.Open, FileAccess.Read)
fsOutput = New System.IO.FileStream(strOutputFile, FileMode.OpenOrCreate, FileAccess.Write)
fsOutput.SetLength(0)
' Currently fails, file not being released for read/write once it's created.
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()
fsInput.Close()
fsOutput.Close()
Catch ex As Exception
errorLog(ex)
End Try
End Sub
推荐答案
连同您传递的参数一起,您要在 EncryptOrDecryptFile()
方法中尝试打开两个流( fsInput
和 fsOutput
)复制到同一文件.打开 fsInput
后,它就具有对该文件的独占访问权限,这使得 fsOutput
无法打开该文件进行写入.
Together with the parameters you pass it, in your EncryptOrDecryptFile()
method you are trying to open two streams (fsInput
and fsOutput
) to the same file. Once you've opened fsInput
it has exclusive access to the file, making fsOutput
unable to open it for writing.
您唯一的选择是:
-
重命名输入文件或输出文件,以便两个流打开两个不同的文件,或者:
Rename either the input file or the output file so that the two streams open two different files, or:
对文件保持全局"加密流,您每次要向其写入数据时都会使用该加密流.
Keep a "global" encryption stream open to the file, which you use every time you want to write data to it.
注意::将来有人可能会建议您使用 FileShare.Write
打开 fsInput
,以便允许其他进程打开文件.用于写入,因此也允许 fsOutput
流也对其进行写入.-不要这样做!
NOTE: Someone in the future might suggest that you open fsInput
with FileShare.Write
in order to allow other processes to open the file for writing, thus also allowing the fsOutput
stream to write to it as well. - DON'T DO THAT!
即使您是 fsOutput
流,在读取文件时允许其他进程写入该文件也可能会导致问题.这是因为您永远无法知道多少数据 fsOutput
将要写入.如果加密数据 比原始数据长 怎么办?这将导致它覆盖 fsInput
接下来要读取的内容,从而损坏文件.
Allowing other processes to write to the file while you're reading from it can cause problems, even if it's your fsOutput
stream. This is because you'll never be able to know how much data fsOutput
is going to write. What if the encrypted data is longer than the original? That would result in it overwriting what fsInput
is going to read next, thus corrupting the file.
这篇关于加密.txt文件错误-进程无法访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!