用这两种方式组合路径有什么区别? [英] What is the difference between combining paths in those 2 ways?

查看:97
本文介绍了用这两种方式组合路径有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能解释一下

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

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

要在Powershell中合并路径吗?

when it comes to combining paths in Powershell?

推荐答案

请考虑以下情况: $ workingDir 带有反斜杠,而 $ fileName 带有反斜杠,例如:

Consider a situation where $workingDir has a trailing backslash and $fileName has a leading one, e.g.:

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

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

The 2 commands will produce the following results:


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

在PowerShell中,最好使用 Join-Path

In PowerShell it's better to use Join-Path:


PS C:\> Join-Path $workingDir $fileName
C:\foo\bar.txt

这篇关于用这两种方式组合路径有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆