【问题标题】:What is the difference between combining paths in those 2 ways?以这两种方式组合路径有什么区别?
【发布时间】:2017-05-19 16:34:04
【问题描述】:

你能解释一下有什么区别吗

$attachment = [String]::Concat($workingDir,"\", $fileName)

$attachment = [IO.Path]::Combine($workingDir, $fileName)

当谈到在 Powershell 中组合路径时?

【问题讨论】:

    标签: .net powershell


    【解决方案1】:

    考虑$workingDir 有一个尾部反斜杠而$fileName 有一个前导反斜杠的情况,例如:

    $workingDir = "C:\foo\"
    $fileName   = "\bar.txt"
    

    这 2 个命令将产生以下结果:

    PS C:\> [String]::Concat($workingDir, "\", $fileName) C:\foo\\\bar.txt PS C:\> [IO.Path]::Combine($workingDir, $fileName) \bar.txt

    在 PowerShell 中最好使用Join-Path

    PS C:\> 加入路径 $workingDir $fileName C:\foo\bar.txt

    【讨论】:

      【解决方案2】:

      Path.Combine 方法在语义上可以识别文件夹路径。例如,如果 $workingDir"c:\",那么 String.Concat 示例将生成带有两个相邻反斜杠的路径。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 2019-03-31
      • 2018-05-18
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多