简体   繁体   中英

Cannot delete file on VB.NET as it is being used by another process

I am creating a Windows Form Application on Visual Basic and I am having some difficulty with deleting a folder. The user selects a file, and that file is first decrypted using DecryptFile() and then previewed on the webbrowser webPreview . I want my program to delete the folder containing the decrypted file (and all the files in the folder, without entering the recycle bin) before it exits. Here is the code for when the file is selected:

Private Sub treFiles_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles treFiles.AfterSelect
    If (Not IO.Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\MyApp\" & lblUsername.Text & "\temp")) Then
        IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\MyApp\" & lblUsername.Text & "\temp")
    End If
    DecryptFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\MyApp\" & lblUsername.Text & "\" & treFiles.SelectedNode.Text,
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\MyApp\" & lblUsername.Text & "\temp\" & treFiles.SelectedNode.Text, "AAAAAAAA")
    webPreview.Url = New Uri(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\MyApp\" & lblUsername.Text & "\temp\" & treFiles.SelectedNode.Text)
End Sub

Here is the DecryptFile() Procedure:

Public Sub DecryptFile(InputFile As String, OutputFile As String, key As String)
    Dim fsInput As New IO.FileStream(InputFile, IO.FileMode.Open, IO.FileAccess.Read)
    Dim fsDecrypted As New IO.FileStream(OutputFile, IO.FileMode.Create, IO.FileAccess.Write)
    Dim DES As New Security.Cryptography.DESCryptoServiceProvider
    DES.Key = Text.ASCIIEncoding.ASCII.GetBytes(key)
    DES.IV = Text.ASCIIEncoding.ASCII.GetBytes(key)
    Dim desdecrypt As Security.Cryptography.ICryptoTransform = DES.CreateDecryptor()
    Dim cryptostream As New Security.Cryptography.CryptoStream(fsInput, desdecrypt, Security.Cryptography.CryptoStreamMode.Read)
    Dim bytearrayinput(fsInput.Length - 1) As Byte
    cryptostream.Read(bytearrayinput, 0, bytearrayinput.Length)
    fsDecrypted.Write(bytearrayinput, 0, bytearrayinput.Length)
    fsInput.Close()
    cryptostream.Close()
    fsDecrypted.Close()
End Sub

And here is the code that executes when the form is exited:

Private Sub frmHome_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
    webPreview.Url = Nothing
    IO.Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\NeedForCrypt\" & lblUsername.Text & "\temp", True)
End Sub

The issue is that when I close the application, Visual Studio gives me this exception:

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file 'test.pdf' because it is being used by another process.

How can I get the files of the temp folder to correctly be deleted on exit?

You need to find out which process is locking the file - try using Microsoft Sysinternals' Process Explorer )

When you have found out which process, you can try ending the process, or if it turns out that the process is your vb program, then you'll need to look closely at your code.

It may well be that the webPreview is locking the file - I think this is probably the case - just before you try to delete the file, try pointing the browser to http://localhost to release the lock on the file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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