FileInfo.BaseName存在在PowerShell中,但不是在直.NET是在、但不、BaseName、FileInfo

由网友(曾经;离去)分享简介:为什么在.NET中的 System.IO.FileInfo 对象没有一个基本名称属性,但我可以通过PowerShell的使用属性,例如:$ FolderItems = GET-ChildItem -PathC:\|位置对象{$ _ -isnot [IO.DirectoryInfo]}的foreach($的FolderI...

为什么在.NET中的 System.IO.FileInfo 对象没有一个基本名称属性,但我可以通过PowerShell的使用属性,例如:

  $ FolderItems = GET-ChildItem -PathC:|位置对象{$ _ -isnot [IO.DirectoryInfo]}

的foreach($的FolderItem在$ FolderItems)
{
    写主机$ FolderItem.BaseName
}
 

解决方案

Powershell的增加,物业为您服务。如果你在一个FileInfo对象运行Get-Member时,你可以看到,这是一个脚本属性,甚至是剧本本身:

 获取项目testfile1.txt |获得会员基本名称|格式列表

类型名:System.IO.FileInfo
名称:基本名称
MemberType:ScriptProperty
定义:System.Object的基本名称{GET = IF($ this.Extension.Length -gt 0){$ this.Name.Remove($ this.Name.Length  - 
         $ this.Extension.Length)}其他{$ this.Name};}
 

代码审计之CVE 2021 40881

Why is it that in .NET the System.IO.FileInfo object does not have a BaseName property, but I can use the property through PowerShell, e.g.:

$FolderItems = Get-ChildItem -Path "C:" | Where-Object {$_ -isnot [IO.DirectoryInfo]}

foreach ($FolderItem in $FolderItems)
{
    write-host $FolderItem.BaseName 
}

解决方案

Powershell adds that property for you. If you run get-member on a fileinfo object, you can see that it's a script property, and even the script itself:

get-item testfile1.txt | Get-Member BaseName | format-list

TypeName   : System.IO.FileInfo
Name       : BaseName
MemberType : ScriptProperty
Definition : System.Object BaseName {get=if ($this.Extension.Length -gt 0){$this.Name.Remove($this.Name.Length - 
         $this.Extension.Length)}else{$this.Name};}

阅读全文

相关推荐

最新文章