【发布时间】: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