![](/img/trans.png)
[英]Powershell: Using the Handle method of an Aggregate Exception in a Try/Catch block
[英]Powershell try block doesn't release file handle for catch block
我正在編寫一個腳本,嘗試使用兩種文件下載方法下載文件。 我不會詳細介紹,但是生成的函數如下所示:
function Download-FileRobust($url, $targetFile) {
try {
Download-File $url $targetFile
}
catch {
Download-FileWget $url $targetFile
}
}
當Download-File
函數失敗時,PowerShell不會在$targetFile
位置松開所創建文件的句柄,並且Download-FileWget
無法寫入該位置。
我已經習慣了Python,所以花了我很長時間才弄清楚問題所在。
其他兩個函數的源代碼如下:
function Download-FileWget($url, $targetFile){
$wgetDir = (Get-ChildItem -Path "$env:userprofile\Downloads\wget*win32").FullName
if($env:Path -notlike "*$wgetDir*"){
$env:Path = "$wgetDir;$env:Path"
}
Invoke-Expression "wget '$url' -O '$targetFile'"
}
function Download-File($url, $targetFile){
$uri = New-Object "System.Uri" "$url"
$request = [System.Net.HttpWebRequest]::Create($uri)
$request.set_Timeout(15000) #15 second timeout
$response = $request.GetResponse()
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
$responseStream = $response.GetResponseStream()
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
$buffer = new-object byte[] 10KB
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $count
while ($count -gt 0){
$targetStream.Write($buffer, 0, $count)
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $downloadedBytes + $count
Write-Progress -activity "Downloading file '$($url.split('/') | Select -Last 1)'" -status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength) * 100)
}
Write-Progress -activity "Finished downloading file '$($url.split('/') | Select -Last 1)'"
$targetStream.Flush()
$targetStream.Close()
$targetStream.Dispose()
$responseStream.Dispose()
}
在Download-File
函數內部,您實際上從未執行任何操作來確保$targetStream
被正確刷新和處理。 您需要將流包裝在try/catch/finally
塊中:
function Download-File {
param([string]$url, [string]$targetFile)
$uri = New-Object "System.Uri" "$url"
$request = [System.Net.HttpWebRequest]::Create($uri)
$request.set_Timeout(15000) #15 second timeout
$response = $request.GetResponse()
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
$responseStream = $response.GetResponseStream()
try {
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
try {
$buffer = new-object byte[] 10KB
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $count
while ($count -gt 0){
$targetStream.Write($buffer, 0, $count)
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $downloadedBytes + $count
Write-Progress -activity "Downloading file '$($url.split('/') | Select -Last 1)'" -status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength) * 100)
}
Write-Progress -activity "Finished downloading file '$($url.split('/') | Select -Last 1)'"
}
catch {
throw
}
finally {
if($targetStream){
$targetStream.Flush()
$targetStream.Close()
$targetStream.Dispose()
}
}
}
catch {
throw
}
finally {
$responseStream.Dispose()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.