当您使用PowerShell中的.NET对象时,它需要一个文件名,它似乎总是相对于C:\Windows\System32.
例如:
[IO.File]::WriteAllText('hello.txt', 'Hello World')
Run Code Online (Sandbox Code Playgroud)
......会写C:\Windows\System32\hello.txt,而不是C:\Current\Directory\hello.txt
为什么PowerShell会这样做?这种行为可以改变吗?如果无法更改,我该如何解决?
我已经尝试了Resolve-Path,但这只适用于已经存在的文件,并且它总是太冗长而无法一直进行.
Den*_*rev 23
您可以更改.NET工作目录到PowerShell的工作目录:
[Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
这条线之后所有.NET方法,如[io.path]::GetFullPath与[IO.File]::WriteAllText将没有任何问题的工作
Kei*_*ill 12
PowerShell不保持当前工作目录的.NET概念与PowerShell工作目录的概念同步的原因是:
为了方便起见,我将以下内容添加到我的prompt函数中,以便它在命令完成时运行:
# Make .NET's current directory follow PowerShell's
# current directory, if possible.
if ($PWD.Provider.Name -eq 'FileSystem') {
[System.IO.Directory]::SetCurrentDirectory($PWD)
}
Run Code Online (Sandbox Code Playgroud)
这不一定是个好主意,因为这意味着某些脚本(假设 Win32 工作目录跟踪 PowerShell 工作目录)可以在我的计算机上运行,但不一定在其他计算机上运行。
| 归档时间: |
|
| 查看次数: |
3596 次 |
| 最近记录: |