测试路径是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