删除 vb.net/framework 2.0 中另一个进程正在使用的文件

Delete a file in use by another process in vb.net / framework 2.0

我有一个程序可以监视某个目录中是否存在具有特定名称的文件。 FileStream 是使用后续命令创建的:

fs = File.Open(PathK, FileMode.Append, FileAccess.Write, FileShare.None)

半小时后,程序关闭,处理并杀死文本文件,然后自行关闭,调用另一个程序创建自己的另一个进程并关闭旧进程。

虽然有时这不会发生。该文件无法删除,它会导致错误。 之后程序自行关闭并继续其重启例程。但是当它自己启动时,它会检查是否有那个特定的文件,它就在那里,因为它无法杀死它。现在,我可以对该文件执行 File.OpenRead,因此旧进程的旧文件流不是 运行,因此这意味着旧进程已关闭。但是我还是杀不死它!

这是起始代码:

Private Sub Verifica_PID_Aperti()
    Dim Tentativi As Integer = 0

    PidAlreadyOpen = 0


    'file exist ?
    If File.Exists(PathK) = True Then

        Try
            'can i read it?
            File.OpenRead(PathK)
        Catch ex As Exception
            'if it's locked than there is another PID active right now, stop this process
            Settaggi.lStop = 1
            PidAlreadyOpen = 1
            Exit Sub
        End Try

        'not locked? than it's a bug and the file is still open, try to delete
        While (Tentativi < 1000)

            Try
                File.Delete(PathK)
                Tentativi = 1000

            Catch ex As Exception
                Tentativi = Tentativi + 1
            End Try
        End While

        'is him still alive?
        If File.Exists(PathK) Then
            'if yes, there is an error, sand a mail and close yourself
            Dim pf As New Send_Mail
            pf.Invio_Mail_Automatico_EDP(999, "ERRORECHIUSURA", Err.Description & " - " & PathK, "", Settaggi.lPID)
            pf = Nothing
            Settaggi.lStop = 1
            PidAlreadyOpen = 1
            Exit Sub
        Else
            'ok, if it does not exists anymore, create another one
            fs = File.Open(PathK, FileMode.Append, FileAccess.Write, FileShare.None)
        End If
    Else
        'no file ? than create it
        fs = File.Open(PathK, FileMode.Append, FileAccess.Write, FileShare.None)
    End If

End Sub

这是重启程序的代码:

    If PidAlreadyOpen = 0 Then
        'close and clean

        fs.Close()
        fs.Dispose()

    End If

    Threading.Thread.Sleep(1000)

    Dim Chiusura As Integer = 0

    While Chiusura < 1000
        Try
            'try to delete

            File.Delete(PathK)

            If TimeToReboot = 1 Then
                System.Diagnostics.Process.Start(Application.ExecutablePath, "/noservice /release /PID:" & Settaggi.lPID)
            End If
        Catch ex As Exception

            Chiusura = Chiusura + 1
            ' not deleted ? send an email, and don't stop yourself
            If Chiusura = 999 Then
                Dim pf As New Send_Mail
                pf.Invio_Mail_Automatico_EDP(999, "ERRORECHIUSURA", Err.Description & " - " & PathK, "", Settaggi.lPID)
                pf = Nothing
                Exit Sub
            End If
            Threading.Thread.Sleep(100)
        End Try
        'deleted? than stop the loop
        If Not File.Exists(PathK) Then
            Chiusura = 1000
        End If

    End While

    Me.Close()

你认为我应该如何解决这个问题?

我认为您在这里无能为力。我过去遇到过类似的问题,发现虽然无法删除文件,但可以重命名文件。根据情况,您可以重命名该文件并使用该名称打开一个新文件。您还需要在稍后的过程中检查重命名的文件是否仍在使用并将其删除。