In PowerShell why does “casting” to FileInfo set the wrong FullName, Directory, and DirectoryName

旧街凉风 提交于 2019-12-05 12:53:04

PowerShell has a notion of your current location. You can see this using the $pwd automatic variable or the Get-Location cmdlet [..]

This path is used by PowerShell to resolve relative paths at the level of the PowerShell API.

[..]

Applications have a notion of the current directory. This is the directory used to resolve relative paths at the level of the Windows API.

How you Get Burned

Your current location may or may not be the same as your current directory.

and

One question that comes up frequently is, “Why does PowerShell not change its [System.Environment]::CurrentDirectory as I navigate around the shell?”

One of the difficult aspects of this comes from the fact that PowerShell supports multiple pipelines of execution. Although it’s not directly exposed yet, users will soon be able to suspend jobs to the background, and other concurrent tasks.

The current directory affects the entire process, so if we change the directory as you navigate around the shell, you risk corrupting the environment of jobs you have running in the background.

When you use filenames in .Net methods, the best practice is to use fully-qualified path names. The Resolve-Path cmdlet makes this easy:

$reader = new-object System.Xml.XmlTextReader (Resolve-Path baseline.xml)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!