測試路徑是fu根本打破。
即使SilentlyContinue被打破:
Test-Path $MyPath -ErrorAction SilentlyContinue
這仍然會炸燬如果$ mypath中爲$ null,爲空或不存在,作爲一個變量。
如果$ MyPath只是一個空格,它甚至會返回$ true。那裏是那個「」文件夾!
下面是在下列情況下工作,解決方法:
$MyPath = "C:\windows" #Test-Path return $True as it should
$MyPath = " " #Test-Path returns $true, Should return $False
$MyPath = "" #Test-Path Blows up, Should return $False
$MyPath = $null #Test-Path Blows up, Should return $False
Remove-Variable -Name MyPath -ErrorAction SilentlyContinue #Test-Path Blows up, Should return $False
解決之道在於迫使它在測試的路徑要炸燬返回$假。
if ($(Try { Test-Path $MyPath.trim() } Catch { $false })) { #Returns $false if $null, "" or " "
write-host "path is GOOD"
} Else {
write-host "path is BAD"
}
我剛得到它的工作,我用[IO.File] ::是否存在(),我認爲,解決路徑將引發同一類型的異常 – 2013-04-09 11:27:23
的我不會改變進程的工作目錄,HTTP ://www.leeholmes.com/blog/2006/06/26/current-working-directory-with-powershell-and-net-calls/ – 2013-04-09 11:55:20
有趣的話,我應該使用[IO.Path] :: GetFullPath( 「$ pwd \ .. \ .. \ myexe.exe」),以避免解決路徑異常,然後File.Exists以避免測試路徑的異常 – 2013-04-09 12:17:52