在ISE中运行此代码的作品。当前目录显然不是当前目录
Push-Location -Path $(Split-Path -Parent $myInvocation.MyCommand.Path)
Get-Location
$file = '.\ex.txt'
$reader = New-Object System.IO.StreamReader($file)
在控制台中运行相同的代码失败。我错过了什么?
PS H:\src\powershell> .\ccount.ps1
Path
----
H:\src\powershell
New-Object : Exception calling ".ctor" with "1" argument(s): "Could not find file
'C:\src\powershell\ex.txt'."
At H:\src\powershell\ccount.ps1:9 char:11
+ $reader = New-Object System.IO.StreamReader($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands
.NewObjectCommand
这是如何从建议的重复
其他问题/答案不同确实给了PowerShell的原因在这种情况下,失败的解释。然而,它并没有给出任何暗示为什么这在ISE中有效。这似乎是控制台和ISE主机之间的重大差异。
我在Windows 7 Enterprise SP1上运行PSVersion 5.0.10586.117。
[为什么PowerShell中的.NET对象不使用当前目录?](https://stackoverflow.com/questions/11246068/why-dont-net-objects-in-powershell-use-the -current-directory) – BenH