HOW TO:获取文件扩展属性

Author: 水如烟

Option   Strict   Off

Namespace  uIO

    
Public   Class  File

        
' 前期绑定的话需引用COM:Microsoft Shell Controls And Automation
         Public   Shared   Function  GetDetails( ByVal  file  As   String As  Dictionary(Of  String Object )
            
Dim  mResult  As   New  Dictionary(Of  String Object )


            
Dim  mPath  As   String   ' 取文件或目录的路径
             Dim  mFileName  As   String    ' 取文件名或目录名

            
If  IO.File.Exists(file)  Then    ' 如是文件
                 Dim  tmpFileInfo  As   New  IO.FileInfo(file)
                
With  tmpFileInfo
                    mPath 
=  .DirectoryName
                    mFileName 
=  .Name
                
End   With
            
Else
                
If  IO.Directory.Exists(file)  Then   ' 如是目录
                     Dim  tmpDirectoryInfo  As   New  IO.DirectoryInfo(file)
                    
With  tmpDirectoryInfo
                        
If  .Parent  Is   Nothing   Then
                            
Throw   New  Exception  ' 不能查询根目录
                         Else
                            mPath 
=  .Parent.FullName
                            mFileName 
=  .Name
                        
End   If
                    
End   With
                
Else
                    
Throw   New  IO.FileNotFoundException  ' 文件或目录无效
                 End   If
            
End   If


            
Dim  mShell  As   Object   ' Shell32.Shell '
             Dim  mFolder  As   Object   ' Shell32.Folder
             Dim  mFolderItem  As   Object   ' Shell32.FolderItem

            mShell 
=   CreateObject ( " Shell.Application " '  New Shell32.Shell '

            
' mFolder = mShell.NameSpace(mPath)
             ' 上语句用WScript解释或引用COM方式都能正常运行, 但后期绑定中返回的是Nothing
             ' 而直接用字符串值代替变量mPath又是能取得正确结果的
             ' 暂用下句代替
            mFolder  =  System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue(Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(mShell,  Nothing " Namespace " New   Object () {mPath},  Nothing Nothing Nothing ))

            mFolderItem 
=  mFolder.ParseName(mFileName)  ' 等效于mFolder.Items.Item(MFileName)

            
' 可能的属性数。一般个数为13,查到扩展属性时是34,这里我加到256
             Dim  mCount  As   Integer   =   & HFF

            
Dim  mPropertyName  As   String
            
Dim  mPropertyValue  As   Object

            
For  i  As   Integer   =   0   To  mCount
                mPropertyName 
=  mFolder.GetDetailsOf( Nothing , i)  ' 取属性名称
                mPropertyValue  =  mFolder.GetDetailsOf(mFolderItem, i)  ' 取属性值
                 If   Not  mPropertyName  =   ""   Then
                    mResult.Add(mPropertyName, mPropertyValue)
                
End   If
            
Next


            mFolderItem 
=   Nothing
            mFolder 
=   Nothing
            System.Runtime.InteropServices.Marshal.ReleaseComObject(mShell)
            mShell 
=   Nothing

            
Return  mResult
        
End Function

    
End Class

End Namespace

测试,顺便也测试混合格式的处理:
     Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
        
Dim  mCollection  As  ICollection  =  LzmTW.uIO.File.GetDetails( " d:\lzmtw.dll " )
        LzmTW.uCollection.MoveNext(mCollection, 
New  Action(Of KeyValuePair(Of  String Object ))( AddressOf  Printer))
    
End Sub

    
Public   Sub  Printer( ByVal  KeyValue  As  KeyValuePair(Of  String Object ))
        
Static  i  As   Integer
        
With  KeyValue
            Console.WriteLine(LzmTW.uString.Format(
" {0,-14}:{1,-30}  第{2,2}项 " , .Key, .Value, i))
        
End   With
        i 
+=   1
    
End Sub

其中MoveNext的代码:
     Public   Shared   Sub  MoveNext(Of T)( ByVal  enumerator  As  IEnumerator,  ByVal  action  As  Action(Of T))

        
While  enumerator.MoveNext
            action.Invoke(
CType (enumerator.Current, T))
        
End   While
    
End Sub

    
Public   Shared   Sub  MoveNext(Of T)( ByVal  collection  As  ICollection,  ByVal  action  As  Action(Of T))

        MoveNext(Of T)(collection.GetEnumerator, action)
    
End Sub

    
Public   Shared   Sub  MoveNext(Of T)( ByVal  enumerable  As  IEnumerable,  ByVal  action  As  Action(Of T))

        MoveNext(Of T)(enumerable.GetEnumerator, action)
    
End Sub

结果,要图片才行:
HOW TO:获取文件扩展属性

你可能感兴趣的:(文件)