HOW TO 獲取檔案擴充套件屬性

2021-04-07 12:35:41 字數 4436 閱讀 2087

author:

水如煙

option

strict

offnamespace

uiopublic

class

file

'前期繫結的話需引用com:microsoft shell controls and automation

public

shared

function

getdetails(

byval

file 

asstring

) as

dictionary(of 

string

, object

)dim

mresult 

asnew

dictionary(of 

string

, object

)dim

mpath 

asstring

'取檔案或目錄的路徑

dimmfilename 

asstring

'取檔名或目錄名

ifio.file.exists(file) 

then

'如是檔案

dimtmpfileinfo 

asnew

io.fileinfo(file)

with

tmpfileinfo

mpath 

=.directoryname

mfilename 

=.name

endwith

else

ifio.directory.exists(file) 

then

'如是目錄

dimtmpdirectoryinfo 

asnew

io.directoryinfo(file)

with

tmpdirectoryinfo

if.parent 

isnothing

then

throw

newexception 

'不能查詢根目錄

else

mpath 

=.parent.fullname

mfilename 

=.name

endif

endwith

else

throw

newio.filenotfoundexception 

'檔案或目錄無效

endif

endif

dimmshell 

asobject

'shell32.shell '

dimmfolder 

asobject

'shell32.folder

dimmfolderitem 

asobject

'shell32.folderitem

mshell 

=createobject("

") 

'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", 

newobject

() , 

nothing

, nothing

, nothing

))mfolderitem 

=mfolder.parsename(mfilename) 

'等效於mfolder.items.item(mfilename)

'可能的屬性數。一般個數為13,查到擴充套件屬性時是34,這裡我加到256

dimmcount 

asinteger=&

hffdim

mpropertyname 

asstring

dimmpropertyvalue 

asobject

fori 

asinteger=0

tomcount

mpropertyname 

=mfolder.getdetailsof(

nothing

, i) 

'取屬性名稱

mpropertyvalue 

=mfolder.getdetailsof(mfolderitem, i) 

'取屬性值

ifnot

mpropertyname =""

then

mresult.add(mpropertyname, mpropertyvalue)

endif

next

mfolderitem 

=nothing

mfolder 

=nothing

system.runtime.interopservices.marshal.releasecomobject(mshell)

mshell 

=nothing

return

mresult

end function

end class

end namespace

測試,順便也測試混合格式的處理:

private

subbutton1_click(

byval

sender 

assystem.object, 

byval

e as

system.eventargs) 

handles

button1.click

dimmcollection 

asicollection 

=lzmtw.uio.file.getdetails(

"d:/lzmtw.dll")

lzmtw.ucollection.movenext(mcollection, 

newaction(of keyvaluepair(of 

string

, object

))(addressof

printer))

end sub

public

subprinter(

byval

keyvalue 

askeyvaluepair(of 

string

, object

))static

i as

integer

with

keyvalue

console.writeline(lzmtw.ustring.format(

":  第項

", .key, .value, i))

endwith

i +=

1end sub

其中movenext的**:

public

shared

submovenext(of t)(

byval

enumerator 

asienumerator, 

byval

action 

asaction(of t))

while

enumerator.movenext

action.invoke(

ctype

(enumerator.current, t))

endwhile

end sub

public

shared

submovenext(of t)(

byval

collection 

asicollection, 

byval

action 

asaction(of t))

movenext(of t)(collection.getenumerator, action)

end sub

public

shared

submovenext(of t)(

byval

enumerable 

asienumerable, 

byval

action 

asaction(of t))

movenext(of t)(enumerable.getenumerator, action)

end sub

結果,要才行:

檔案擴充套件屬性

擴充套件屬性ea,既以名稱 值對形式將任意後設資料與檔案i 節點關聯起來的技術 在shell中操作 root bogon code setfattr n user.x v this is first linux root bogon code setfattr n user.y v this is ...

linux檔案擴充套件屬性

擴充套件屬性 xattrs 提供了一個機制用來將 鍵 值 對永久地關聯到檔案,讓現有的檔案系統得以支援在原始設計中未提供的功能。擴充套件屬性是檔案系統不可知論者,應用程式可以通過一個標準的介面來操縱他們,此介面不因檔案系統而異。每個擴充套件屬性可以通過唯一的鍵來區分,鍵的內容必須是有效的utf 8,...

Qt獲取檔案屬性

在qt中有qfileinfo類專門提供了獲取檔案資訊的各種介面,比如檔名稱,位置資訊,檔案的許可權,目錄,檔案或符號連線,檔案大小,建立時間,最後修改時等等,下面通過 來看一些具體的屬性獲取。ifndef mainwindow h define mainwindow h include qt beg...

獲取檔案屬性函式

表標頭檔案 include 函式定義 int stat const char file name,struct stat buf 函式說明 通過檔名filename獲取檔案資訊,並儲存在buf所指的結構體stat中 返回值 執行成功則返回0,失敗返回 1,錯誤 存於errno 需要include e...

常用檔案屬性獲取

朱老師物聯網大講堂 學習筆記 檔案屬性,檔案的屬性資訊,只能被專用的api開啟看到,常用的api有,stat,fstat,lstat,同時stat也是一個shell命令,其內部呼叫的也是stat,int stat const char path,struct stat buf int fstat i...