在调查一些问题的过程中,我发现原因是意外不同的转换为看似相同的输入数据的 string[]。即,在下面的代码中,两个命令都返回相同的两个项 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