Search code examples
vb.netexceptionsystem.net.webexception

Can get process ID from exception?


Attempting to deploy updates to a .dll using WebClient.DownloadFile. If the dll is loaded/locked by the program it cannot be overwritten, so i'm trying to use a Try... Catch statement (on the following exception) to curate the Process ID and .Dispose() of it.

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. 

This may or may not be the best methodology... below is my code. Any pointers much appreciated!

        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

Solution

  • If anyone else is interested, I never figured out how to get the process ID from the exception (I don't think it's possible) but I discovered that it may not be necessary...

    What I'm doing instead is just renaming the DLL if it's locked (everything seems to still run as expected even after rename). After it's renamed, the updated DLL can be downloaded to the standard location for the live DLL.