简体   繁体   English

在PowerShell中,为什么“转换”到FileInfo会设置错误的FullName,Directory和DirectoryName

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

I'm noticing a VERY odd behavior in both my PS ISE and PS when I'm getting a FileInfo object by calling 当我通过调用获取FileInfo对象时,我注意到我的PS ISE和PS都有一个非常奇怪的行为

$FileInfo = [System.IO.FileInfo](".\SomeFile.ext")

When I look at its properties the DirectoryName and its related properties all default to the ORIGINAL path that PS opens in. 当我查看其属性时, DirectoryName及其相关属性都默认为PS打开的ORIGINAL路径。

Here's a copy from my ISE which launches as an administrator with c:\\Windows\\System32 as the default path 这是我的ISE的副本,它以管理员身份启动, c:\\Windows\\System32作为默认路径

If I run the following code: 如果我运行以下代码:

$Fileinfo = [System.IO.FileInfo](".\@OpenWithToastLogo.png")  
cd c:\temp  
$Fileinfo2 = [System.IO.FileInfo](".\activation.txt")  
z:  
$Fileinfo3 = [System.IO.FileInfo](".\7za.exe")  
$Fileinfo | fl *
$Fileinfo2 | fl *
$Fileinfo3 | fl *

I get 我明白了

VersionInfo : 版本信息 :
File : C:\\WINDOWS\\system32\\@OpenWithToastLogo.png 文件:C:\\ WINDOWS \\ system32 \\ @ OpenWithToastLogo.png
InternalName : 内部名称:
OriginalFilename : OriginalFilename:
FileVersion : FileVersion:
FileDescription : 文件描述 :
Product : 产品:
ProductVersion : 产品版本:
Debug : False 调试:错误
Patched : False 补丁:错误
PreRelease : False 预发布:错误
PrivateBuild : False PrivateBuild:错误
SpecialBuild : False SpecialBuild:错误
Language : 语言 :
BaseName : @OpenWithToastLogo BaseName:@OpenWithToastLogo
Target : {C:\\Windows\\WinSxS\\wow64_microsoft-windows-openwith_31bf3856ad364e35_10.0.10240.16384_none_7f75eaad41c1f239\\@OpenWithToastLogo.png,C:\\Windows\\SysWOW64\\@OpenWithToastLogo.png} 目标:{C:\\ Windows \\ WinSxS \\ wow64_microsoft-windows-openwith_31bf3856ad364e35_10.0.10240.16384_none_7f75eaad41c1f239 \\ @ OpenWithToastLogo.png,C:\\ Windows \\ SysWOW64 \\ @ OpenWithToastLogo.png}
LinkType : HardLink LinkType:HardLink
Mode : -a---l 模式:-a --- l
Name : @OpenWithToastLogo.png 姓名:@ OpenWithToastLogo.png
Length : 160 长度:160
DirectoryName : C:\\WINDOWS\\system32 DirectoryName:C:\\ WINDOWS \\ system32
Directory : C:\\WINDOWS\\system32 目录:C:\\ WINDOWS \\ system32
IsReadOnly : False IsReadOnly:错
Exists : True 存在:真的
FullName : C:\\WINDOWS\\system32\\@OpenWithToastLogo.png FullName:C:\\ WINDOWS \\ system32 \\ @ OpenWithToastLogo.png
Extension : .png CreationTime : 7/10/2015 7:00:32 AM 扩展名:.png CreationTime:7/10/2015 7:00:32 AM
CreationTimeUtc : 7/10/2015 11:00:32 AM CreationTimeUtc:7/10/2015 11:00:32 AM
LastAccessTime : 7/10/2015 7:00:32 AM LastAccessTime:7/10/2015 7:00:32 AM
LastAccessTimeUtc : 7/10/2015 11:00:32 AM LastAccessTimeUtc:7/10/2015 11:00:32 AM
LastWriteTime : 7/10/2015 7:00:32 AM LastWriteTime:7/10/2015 7:00:32 AM
LastWriteTimeUtc : 7/10/2015 11:00:32 AM LastWriteTimeUtc:7/10/2015 11:00:32 AM
Attributes : Archive 属性:存档

VersionInfo : 版本信息 :
BaseName : activation Target : BaseName:激活目标:
LinkType : LinkType:
Mode : darhsl 模式:darhsl
Name : activation.txt 名称:activation.txt
Length : 长度 :
DirectoryName : C:\\WINDOWS\\system32 DirectoryName:C:\\ WINDOWS \\ system32
Directory : C:\\WINDOWS\\system32 目录:C:\\ WINDOWS \\ system32
IsReadOnly : True IsReadOnly:是的
Exists : False 存在:错误
FullName : C:\\WINDOWS\\system32\\activation.txt FullName:C:\\ WINDOWS \\ system32 \\ activation.txt
Extension : .txt 扩展名:.txt
CreationTime : 12/31/1600 7:00:00 PM CreationTime:12/31/1600 7:00:00 PM
CreationTimeUtc : 1/1/1601 12:00:00 AM CreationTimeUtc:1/1/1601 12:00:00 AM
LastAccessTime : 12/31/1600 7:00:00 PM LastAccessTime:12/31/1600 7:00:00 PM
LastAccessTimeUtc : 1/1/1601 12:00:00 AM LastAccessTimeUtc:1/1/1601 12:00:00 AM
LastWriteTime : 12/31/1600 7:00:00 PM LastWriteTime:12/31/1600 7:00:00 PM
LastWriteTimeUtc : 1/1/1601 12:00:00 AM LastWriteTimeUtc:1/1/1601 12:00:00 AM
Attributes : -1 属性:-1

VersionInfo : 版本信息 :
BaseName : BaseName:
7za Target : 7za目标:
LinkType : LinkType:
Mode : darhsl 模式:darhsl
Name : 7za.exe 名称:7za.exe
Length : 长度 :
DirectoryName : C:\\WINDOWS\\system32 DirectoryName:C:\\ WINDOWS \\ system32
Directory : C:\\WINDOWS\\system32 目录:C:\\ WINDOWS \\ system32
IsReadOnly : True IsReadOnly:是的
Exists : False 存在:错误
FullName : C:\\WINDOWS\\system32\\7za.exe FullName:C:\\ WINDOWS \\ system32 \\ 7za.exe
Extension : .exe 扩展名:.exe
CreationTime : 12/31/1600 7:00:00 PM CreationTime:12/31/1600 7:00:00 PM
CreationTimeUtc : 1/1/1601 12:00:00 AM LastAccessTime : 12/31/1600 7:00:00 PM CreationTimeUtc:1/1/1601 12:00:00 AM LastAccessTime:12/31/1600 7:00:00 PM
LastAccessTimeUtc : 1/1/1601 12:00:00 AM LastAccessTimeUtc:1/1/1601 12:00:00 AM
LastWriteTime : 12/31/1600 7:00:00 PM LastWriteTime:12/31/1600 7:00:00 PM
LastWriteTimeUtc : 1/1/1601 12:00:00 AM LastWriteTimeUtc:1/1/1601 12:00:00 AM
Attributes : -1 属性:-1

With my regular account it defaults to my H:\\ path so the same test would have H:\\ as the DirectoryName . 使用我的常规帐户,它默认为我的H:\\路径,因此相同的测试将具有H:\\作为DirectoryName

And yes, each of these files ONLY exists in the directory it was relative to. 是的,这些文件中的每一个都只存在于它相对的目录中。

Has anyone seen this before, and do they know a good fix? 有没有人见过这个,他们知道一个很好的解决方案吗? Using fully qualified of course works but this is for a script that other technicians might run and we're all pretty used to the idea of using relative pathing with PS. 使用完全合格的课程作品,但这是针对其他技术人员可能运行的脚本,我们都习惯于使用PS的相对路径。

PowerShell has a notion of your current location. PowerShell有一个您当前位置的概念。 You can see this using the $pwd automatic variable or the Get-Location cmdlet [..] 你可以使用$pwd自动变量或Get-Location cmdlet [..]来看到这个

This path is used by PowerShell to resolve relative paths at the level of the PowerShell API. PowerShell使用此路径来解析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. 这是用于解析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?” 经常出现的一个问题是,“当我在shell中导航时,为什么PowerShell不会更改其[System.Environment]::CurrentDirectory ?”

One of the difficult aspects of this comes from the fact that PowerShell supports multiple pipelines of execution. 其中一个困难的方面来自PowerShell支持多个执行管道的事实。 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. 当前目录会影响整个过程,因此如果我们在shell中导航时更改目录,则可能会破坏您在后台运行的作业的环境。

When you use filenames in .Net methods, the best practice is to use fully-qualified path names. 在.Net方法中使用文件名时,最佳做法是使用完全限定的路径名​​。 The Resolve-Path cmdlet makes this easy: Resolve-Path cmdlet使这很容易:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM