相关疑难解决方法(0)

神秘的不同转换为看似相同的输入数据的字符串[]

在调查某些问题时,我发现原因是看似相同的输入数据的字符串[]的意外不同转换.也就是说,在下面的代码中,两个命令都返回相同的两个项File1.txt和File2.txt.但转换为string []会产生不同的结果,请参阅注释.

任何想法为什么呢?这可能是一个错误.如果有人也这么认为,我会提交.但是,了解正在发生的事情并避免陷阱就好了.

# *** WARNING
# *** Make sure you do not have anything in C:\TEMP\Test
# *** The code creates C:\TEMP\Test with File1.txt, File2.txt

# Make C:\TEMP\Test and two test files
$null = mkdir C:\TEMP\Test -Force
1 | Set-Content C:\TEMP\Test\File1.txt
1 | Set-Content C:\TEMP\Test\File2.txt

# This gets just file names
[string[]](Get-ChildItem C:\TEMP\Test)

# This gets full file paths
[string[]](Get-ChildItem C:\TEMP\Test -Include *)

# Output:
# File1.txt
# File2.txt
# C:\TEMP\Test\File1.txt
# C:\TEMP\Test\File2.txt
Run Code Online (Sandbox Code Playgroud)

powershell powershell-2.0

3
推荐指数
1
解决办法
1587
查看次数

标签 统计

powershell ×1

powershell-2.0 ×1