简体   繁体   English

vb.net 上的文件删除异常

[英]file deletion exception on vb.net

My VB.NET junk cleaner cannot delete aria-Debug-10144.log since it is used by another process.我的 VB.NET 垃圾清理器无法删除 aria-Debug-10144.log,因为它被另一个进程使用。

I tried this:我试过这个:

Imports System.Collections.ObjectModel
Imports System.IO

Public Class Form1
    Dim TempDirs As ReadOnlyCollection(Of String)
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TempDirs = (My.Computer.FileSystem.GetDirectories("C:\Users\Aitor\AppData\Local\Temp"))
        Dim ListDirs As List(Of String) = TempDirs.ToList
        Dim directoryName As String = "C:\Users\Aitor\AppData\Local\Temp"
        For Each deleteFile In Directory.GetFiles(directoryName, "*.*", SearchOption.TopDirectoryOnly)
            If Not deleteFile.ToString = "aria-Debug-10144.log" Then
                File.Delete(deleteFile)
            End If
        Next
        MsgBox("Clean completed!", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Results")
    End Sub
End Class

But it still tries to do it.但它仍然试图做到这一点。

Can anybody help me?有谁能够帮助我?

There are two reasons:有两个原因:

Firstly, the selected file is being occupied by another program.首先,所选文件正被另一个程序占用。 In this case, you need to determine whether the selected file is occupied before deleting it.在这种情况下,您需要在删除之前确定所选文件是否被占用。 Here is a small example.这是一个小例子。

Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("kernel32.dll")>
    Public Shared Function _lopen(ByVal lpPathName As String, ByVal iReadWrite As Integer) As IntPtr

    End Function
    <DllImport("kernel32.dll")>
    Public Shared Function CloseHandle(ByVal hObject As IntPtr) As Boolean

    End Function

    Public Const OF_READWRITE As Integer = 2
    Public Const OF_SHARE_DENY_NONE As Integer = &H40
    Public Shared ReadOnly HFILE_ERROR As IntPtr = New IntPtr(-1)

    Public Shared Function IsFileOccupied(ByVal filePath As String) As Boolean
        Dim vHandle As IntPtr = _lopen(filePath, OF_READWRITE Or OF_SHARE_DENY_NONE)
        CloseHandle(vHandle)
        Return If(vHandle = HFILE_ERROR, True, False)
    End Function


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If (IsFileOccupied("C:\Users\juliex\AppData\Local\Temp\aria-debug-13836.log")) Then
            MessageBox.Show("File is already occupied")
        Else
            MessageBox.Show("File is not occupied")
            'Then do some delete operation.
        End If
    End Sub
End Class

Secondly, the selected file is a temporary file and is currently locked.其次,所选文件是临时文件,当前已锁定。 If you want to clear the temporary files it owns, you need to have full control to unlock these files and then delete them!如果你想清除它拥有的临时文件,你需要有完全的控制权来解锁这些文件然后删除它们! You should be very careful if you want to delete the TEMP file, whether it is owned by the application or by another owner.如果要删除 TEMP 文件,无论它是由应用程序拥有还是由其他所有者拥有,都应该非常小心。 The original application may have applied the lock because it wanted to use the file!原始应用程序可能已经应用了锁定,因为它想使用该文件! At this time you can refer to this link below:这个时候可以参考下面这个链接:

1. How can I unlock a file that is locked by a process in .NET 1. 如何解锁被.NET进程锁定的文件

2. https://social.msdn.microsoft.com/Forums/vstudio/en-US/9e2044c5-ae5d-4552-a335-01cc567dfc58/how-to-unlock-a-file-used-by-other-process?forum=csharpgeneral 2. https://social.msdn.microsoft.com/Forums/vstudio/en-US/9e2044c5-ae5d-4552-a335-01cc567dfc58/how-to-unlock-a-file-used-by-other-process?论坛=csharpgeneral

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

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