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

结果,要图片才行:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值