获取扩展的文件信息 [英] Get extended file information details

查看:178
本文介绍了获取扩展的文件信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用VB.net获取windows文件的详细信息?



我的意思是指在右键单击文件时发现的细节类型,说一个MS Word文档,然后单击属性,然后选择详细信息选项卡。

我知道有些可以通过FileInfo获得,但不是全部,比如Tags。
谢谢

解决方案

对于那些你需要使用Shell32的东西。从COM选项卡中,找到并添加 Microsoft Shell控件和自动化。这里是创建一个给定文件的属性值列表的代码:

 'class to持有好东西
朋友类ShellInfo
公共财产名称作为字符串
公共财产值作为字符串

公共子新(n作为字符串,v作为字符串)
Name = n
Value = v
End Sub

公共覆盖函数ToString()As String
返回名称

End Function
End Class

然后填写一个函数

  Private Function GetXtdShellInfo(filepath As String)As List(Of ShellInfo)
'ToDo:添加错误检查,也许Try / Catch和
'肯定检查文件是否存在,然后再尝试
Dim xtd作为新列表(ShellInfo)

昏暗的壳作为新的Shell32.Shell
昏暗的shFolder作为Shell32.Folder
shFolder = shell.NameSpace(Path.GetDirectoryName(filepath))

'它的com如此迭代以找到我们想要的 -
'或者修改以返回所有项目的列表字典
Dim key As String

For Each s在shFolder.Items
'寻找一个我们在
之后如果shfolder.GetDetailsOf(s,0).ToLowerInvariant = Path.GetFileName(file).ToLowerInvariant然后

Dim ndx As Int32 = 0
key = shfolder.GetDetailsOf(shfolder.Items,ndx)

'根据操作系统
有不同数量的条目'34分钟,W7 = 290,W8 = 309有一些空白

'这应该得到310非空白元素

直到String.IsNullOrEmpty(key)AndAlso ndx > 310
如果String.IsNullOrEmpty(key)= False那么
xtd.Add(新ShellInfo(key,
shfolder.GetDetailsOf(s,ndx)))
End If
ndx + = 1
key = shfolder.GetDetailsOf(shfolder.Items,ndx)
Loop

'我们得到了我们来的
Exit
结束如果
下一个

返回xtd
结束功能

使用它很简单:

  Dim xtd As List(Of ShellInfo)= GetXtdShellInfo(C:\Temp \Capri.jpg)
对于每个s作为ShellInfo在xtd
Console.WriteLine({0}:{1},s.Name,s.Value)
Next

返回值应该是 ShellInfo 项目的列表名称是 Name,BitRate,Album 和相关联的 Value 的属性名称将返回 SHELL32 。例如

 名称:Capri.jpg 
大小:15.2 KB
项目类型:图像文件
修改日期:7/20/2014 12:19 PM
创建日期:7/20/2014 12:17 PM
访问日期:2014/7/20 12:17
(等等)

实际返回的数字会因操作系统版本而异
如注释 Microsoft Shell控制和自动化更改为 Microsoft Shell文件夹查看路由器 (在Windows 8.1中)。

另外,前35个属性是相当知名的,并且更常见,但是Win7有291个。在Windows 8下,最大值是309有一些空白点,并深入到名单中,一些房地产指数发生了变化。

查看与这个答案相关的问题如何从a中读取比特率信息。 mov视频文件标题


How can one obtain the details of a windows file using VB.net?

The type of details I mean are those found when I right click on a file, say an MS word doc, then click "Properties" and select the "Details" tab.

I know some can be obtained via FileInfo, but not all, such as "Tags" for example. Thanks

解决方案

For that stuff you need to use Shell32. From the COM tab, find and add Microsoft Shell Controls and Automation. Here is code to create a list of property-values for a given file:

' class to hold the goodies
Friend Class ShellInfo
    Public Property Name As String
    Public Property Value As String

    Public Sub New(n As String, v As String)
        Name = n
        Value = v
    End Sub

    Public Overrides Function ToString() As String
        Return Name

    End Function
End Class

Then a function to fill it up

Private Function GetXtdShellInfo(filepath As String) As List(Of ShellInfo)
    ' ToDo: add error checking, maybe Try/Catch and 
    ' surely check if the file exists before trying
    Dim xtd As New List(Of ShellInfo)

    Dim shell As New Shell32.Shell
    Dim shFolder As Shell32.Folder
    shFolder = shell.NameSpace(Path.GetDirectoryName(filepath))

    ' its com so iterate to find what we want -
    ' or modify to return a dictionary of lists for all the items
    Dim key As String

    For Each s In shFolder.Items
        ' look for the one we are after
        If shfolder.GetDetailsOf(s, 0).ToLowerInvariant = Path.GetFileName(file).ToLowerInvariant Then

            Dim ndx As Int32 = 0
            key = shfolder.GetDetailsOf(shfolder.Items, ndx)

            ' there are a varying number of entries depending on the OS
            ' 34 min, W7=290, W8=309 with some blanks

            ' this should get up to 310 non blank elements

            Do Until String.IsNullOrEmpty(key) AndAlso ndx > 310
                If String.IsNullOrEmpty(key) = False Then
                    xtd.Add(New ShellInfo(key,
                                          shfolder.GetDetailsOf(s, ndx)))
                End If
                ndx += 1
                key = shfolder.GetDetailsOf(shfolder.Items, ndx)
            Loop

            ' we got what we came for
            Exit For
        End If
    Next

    Return xtd
End Function

Using it is simple:

Dim xtd As List(Of ShellInfo) = GetXtdShellInfo("C:\Temp\Capri.jpg")
For Each s As ShellInfo In xtd
    Console.WriteLine("{0}: {1}", s.Name, s.Value)
Next

The return should be a list of ShellInfo items where the Name is the property name such as Name, BitRate, Album and the associated Value will be that returned by Shell32. e.g

 Name: Capri.jpg
 Size: 15.2 KB
 Item type: Image File
 Date modified: 7/20/2014 12:19 PM
 Date created: 7/20/2014 12:17 PM
 Date accessed: 7/20/2014 12:17 PM
 (etc)

The actual number returned will vary depending on the OS ver


As noted in the comment Microsoft Shell Controls and Automation is renamed as Microsoft Shell Folder View Router (in Windows 8.1).

Also, the first 35 properties are fairly well known and more common, but with Win7 there are about 291. Under Windows 8, the max is 309 with some blank spots and deep into the list some property indices are changed.

See this answer related question How to read the bit rate information from a .mov video file header

这篇关于获取扩展的文件信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆