可以从异常中获取进程ID吗?

时间:2018-04-13 14:54:04

标签: vb.net exception-handling system.net.webexception

尝试使用WebClient.DownloadFile将更新部署到.dll。如果程序加载/锁定了DLL,则无法覆盖它,因此我尝试使用Try ... Catch语句(在以下异常中)来管理它的进程ID和.Dispose()。

System.Net.WebException: 'An exception occurred during a WebClient request.'

Inner Exception 
IOException: The process cannot access the file 'xyz' because it is being used by another process. 

这可能是也可能不是最好的方法......下面是我的代码。任何指针都非常赞赏!

        Try
            Using WC As New WebClient
                WC.DownloadFile("https://urlgoeshere.com/library.dll", strLiveDLL)
            End Using

        Catch ex1 As System.Net.WebException
            Using P As Process = ex1.WhatGoesHere 'can get the process ID here??
                If MsgBox("Cannot update because dll file is locked by " & P.ProcessName & vbCr &
                   "Press OK to dispose of this process and continue with update.",
                    MsgBoxStyle.OkCancel & MsgBoxStyle.Question,
                   "Update Interrupted") = MsgBoxResult.Ok Then
                    P.Dispose()
                    'continue with update
                Else
                    MsgBox("Update Aborted.")
                End If

            End Using
        Catch ex2 As IOException
            '
        Catch ex3 As Exception
            '
        End Try

1 个答案:

答案 0 :(得分:0)

如果有其他人感兴趣,我从未想过如何从例外中获取进程ID(我不认为这是可能的)但我发现它可能没有必要... < / p>

我正在做的只是重命名DLL,如果它被锁定(即使重命名后,一切似乎仍按预期运行)。重命名后,可以将更新的DLL下载到实时DLL的标准位置。