简体   繁体   English

该过程无法访问文件[VB.Net]

[英]The process cannot access the file[VB.Net]

I'm trying to save a file, then crypt is and delete the temporary uncrypted file. 我正在尝试保存文件,然后选择crypt并删除临时的未加密文件。 This is my encryption sub, the error line occurs on the last line. 这是我的加密子项,错误行出现在最后一行。

Sub EncryptFile(ByVal sInputFilename As String, _
              ByVal sOutputFilename As String, _
              ByVal sKey As String)

    Dim fsInput As New FileStream(sInputFilename, _
                                FileMode.Open, FileAccess.Read)
    Dim fsEncrypted As New FileStream(sOutputFilename, _
                                FileMode.Create, FileAccess.Write)

    Dim DES As New DESCryptoServiceProvider()

    'Set secret key for DES algorithm.
    'A 64-bit key and an IV are required for this provider.
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)

    'Set the initialization vector.
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

    'Create the DES encryptor from this instance.
    Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
    'Create the crypto stream that transforms the file stream by using DES encryption.
    Dim cryptostream As New CryptoStream(fsEncrypted, _
                                        desencrypt, _
                                        CryptoStreamMode.Write)

    'Read the file text to the byte array.
    Dim bytearrayinput(fsInput.Length - 1) As Byte
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
    'Write out the DES encrypted file.
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
    cryptostream.Close()
    System.IO.File.Delete(sInputFilename)
End Sub

Can anyone help me out with this? 谁能帮我这个忙吗? I can't figure out what I am doing wrong. 我不知道自己在做什么错。

Use fsInput.Close() right before System.IO.File.Delete(sInputFilename) System.IO.File.Delete(sInputFilename)之前使用fsInput.Close() System.IO.File.Delete(sInputFilename)

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM