【问题标题】:Can get process ID from exception?可以从异常中获取进程ID吗?
【发布时间】:2018-04-13 14:54:04
【问题描述】:

尝试使用 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

【问题讨论】:

  • 您可以使用this sample project 中的代码来查找锁定文件的进程,您只需将 args() 替换为包含文件路径的字符串
  • 谢谢,shoohonigan。我希望有一种更简洁的方法来管理锁定文件的进程。

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


【解决方案1】:

如果其他人有兴趣,我从来不知道如何从异常中获取进程 ID(我认为不可能)但我发现它可能没有必要......

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-09
    • 2012-10-12
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    相关资源
    最近更新 更多