gpt4 book ai didi

powershell - 如何在 PowerShell 中更改为临时目录?

转载 作者:行者123 更新时间:2023-12-02 11:25:30 24 4
gpt4 key购买 nike

在 PowerShell 中,我可以回显 %TEMP% 的值使用命令 $Env:TEMP .这是我机器上的输出:

PS> $Env:temp
C:\Users\IAIN~1.COR\AppData\Local\Temp

当我尝试使用 cd 更改目录时命令,我收到此错误:
PS> cd $Env:temp
Set-Location : An object at the specified path C:\Users\IAIN~1.COR does not exist.
At line:1 char:3
+ cd <<<< $Env:temp
+ CategoryInfo : InvalidArgument: (:) [Set-Location], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.SetLocationCommand

我怀疑 PowerShell 正在逐字解释 8.3 文件名。目录的长文件名是 C:\Users\iain.CORP\AppData\Local\Temp .当我尝试时 cd C:\Users\Iain.CORP\AppData\Local\Temp ,目录更改成功。

如何打开 $Env:TEMP中的路径使用 PowerShell?我必须先有长文件名吗?

最佳答案

您不需要访问 %TEMP%直接使用环境变量。

.NET 提供了一个 GetTempPath方法作为更通用的解决方案。

$TempDir = [System.IO.Path]::GetTempPath()
cd $TempDir

在我的机器上,这将更改为目录 C:\Users\Iain.CORP\AppData\Local\Temp .

备注来自 the documentation :

This method checks for the existence of environment variables in the following order and uses the first path found:

  1. The path specified by the TMP environment variable.

  2. The path specified by the TEMP environment variable.

  3. The path specified by the USERPROFILE environment variable.

  4. The Windows directory.



感谢 Joe Angley分享技术。

关于powershell - 如何在 PowerShell 中更改为临时目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10782739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com