2011-02-08 86 views
33

当我运行的PowerShell上的目录(或返回文件系统项目的任何小命令)的Get-ChildItem,它显示了一个叫Mode列,例如:PowerShell的Get-ChildItem cmdlet返回可能的'Mode'值是什么?

Directory: C:\MyDirectory 


Mode    LastWriteTime  Length Name 
----    -------------  ------ ---- 
d----   2/8/2011 10:55 AM   Directory1 
d----   2/8/2011 10:54 AM   Directory2 
d----   2/8/2011 10:54 AM   Directory3 
-ar--   2/8/2011 10:54 AM  454 File1.txt 
-ar--   2/8/2011 10:54 AM  4342 File2.txt 

我找啊找谷歌和我的本地PowerShell的书,但我找不到关于Mode列含义的任何文档。

Mode列的可能值是什么?每个值的含义是什么?

回答

38

请注意,您看到模式只是一个位域enum,在Attributes财产隐藏的字符串表示。你可以通过简单地显示并排两侧找出单个字母的意思是:

PS> gci|select mode,attributes -u 

Mode    Attributes 
----    ---------- 
d-----    Directory 
d-r---  ReadOnly, Directory 
d----l Directory, ReparsePoint 
-a----     Archive 

在任何情况下,完整列表是:

d - Directory 
a - Archive 
r - Read-only 
h - Hidden 
s - System 
l - Reparse point, symlink, etc. 
6

恕我直言,最具解释是代码本身:

if (instance == null) 
{ 
    return string.Empty; 
} 
FileSystemInfo baseObject = (FileSystemInfo) instance.BaseObject; 
if (baseObject == null) 
{ 
    return string.Empty; 
} 
string str = ""; 
if ((baseObject.Attributes & FileAttributes.Directory) == FileAttributes.Directory) 
{ 
    str = str + "d"; 
} 
else 
{ 
    str = str + "-"; 
} 
if ((baseObject.Attributes & FileAttributes.Archive) == FileAttributes.Archive) 
{ 
    str = str + "a"; 
} 
else 
{ 
    str = str + "-"; 
} 
if ((baseObject.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) 
{ 
    str = str + "r"; 
} 
else 
{ 
    str = str + "-"; 
} 
if ((baseObject.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) 
{ 
    str = str + "h"; 
} 
else 
{ 
    str = str + "-"; 
} 
if ((baseObject.Attributes & FileAttributes.System) == FileAttributes.System) 
{ 
    return (str + "s"); 
} 
return (str + "-"); 
+4

咦?你从哪里得到的代码? – 2011-02-09 00:10:31

+1

@splattered:PowerShell程序集中的反射器,我猜。 – Joey 2011-02-09 00:14:14

2

这些所有文件属性的名称和有含义可以发现here

PS C:\> [enum]::GetNames("system.io.fileattributes") 
ReadOnly 
Hidden 
System 
Directory 
Archive 
Device 
Normal 
Temporary 
SparseFile 
ReparsePoint 
Compressed 
Offline 
NotContentIndexed 
Encrypted 
0

调用这些“属性”是一个Windows特定的名称,并从* nix调用此“模式”的传统中断。即man chmod“改变模式”。

它看起来像Windows API设计弯曲(或默认)在更广泛的行业中更流行的术语:“模式”

+1 from me。

相关问题