代码之家  ›  专栏  ›  技术社区  ›  Josh Buedel

为什么PowerShell从$home而不是当前目录解析路径?

  •  14
  • Josh Buedel  · 技术社区  · 14 年前

    [System.IO.Path]::GetFullPath(".\foo.txt")
    

    但事实并非如此。它印着。。。

    C:\Documents and Settings\Administrator\foo.txt
    

    我不在$home目录中。为什么会在那里解决?

    2 回复  |  直到 14 年前
        1
  •  18
  •   user7116    13 年前

    [System.IO.Path] 正在使用shell进程的当前目录。你可以得到绝对路径 Resolve-Path

    Resolve-Path .\foo.txt
    
        2
  •  12
  •   zdan    14 年前

    根据文件 GetFullPath ,它使用当前工作目录来解析绝对路径。powershell当前工作目录与当前位置不同:

    PS C:\> [System.IO.Directory]::GetCurrentDirectory()
    C:\Documents and Settings\user
    PS C:\> get-location
    
    Path
    ----
    C:\
    

    我想您可以使用SetCurrentDirectory来匹配它们:

    PS C:\> [System.IO.Directory]::SetCurrentDirectory($(get-location))
    PS C:\> [System.IO.Path]::GetFullPath(".\foo.txt")
    C:\foo.txt