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

时间:2018-06-11 12:36:00

标签: vb.net process delete-file

我有一个程序可以监视某个目录中是否有某个名称的文件。 使用sequent命令创建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()

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

1 个答案:

答案 0 :(得分:0)

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