如何使用VB.net获取windows文件的详细信息?
我指的是当我右键单击一个文件,比如MS word文档,然后单击“属性”并选择“详细信息”选项卡时找到的详细信息类型。
我知道有些可以通过FileInfo获得,但不是全部,比如“标签”。谢谢
发布于 2014-10-01 23:31:35
对于这些东西,你需要使用Shell32。从COM选项卡中,找到并添加Microsoft Shell控件和自动化。下面是为给定文件创建属性值列表的代码:
' 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然后是一个填充它的函数
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使用它很简单:
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返回的应该是ShellInfo项的列表,其中名称是属性名,例如Name, BitRate, Album,关联的Value将是由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)根据操作系统版本的不同,返回的实际数字会有所不同
正如注释中所指出的,微软外壳控制和自动化被重命名为微软外壳文件夹视图路由器(在Windows8.1中)。
此外,前35个属性是相当知名的,而且更常见,但对于Win7,大约有291个属性。在Windows8下,最大值是309,其中有一些空白点,并且在列表的深层,一些属性索引发生了变化。
请参阅此答案相关问题How to read the bit rate information from a .mov video file header
发布于 2020-11-13 16:51:01
只需使用类似以下内容:
Dim MyFileInfos as System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(PathToYourFile)然后使用MyFileInfo.* (无论您需要什么,都可以使用IntelliSense)获取信息。
祝您今天愉快
发布于 2021-01-28 21:18:28
Dim shellAppType = Type.GetTypeFromProgID("Shell.Application")
Dim shellApp = Activator.CreateInstance(shellAppType)
Dim folder = shellApp.NameSpace("c:\users\frank")
Dim folderitem = folder.parsename("yourfile.jpg")
Dim value = folder.GetDetailsOf(folderitem, 24) 'eg. 24 retrieves File Comments.https://stackoverflow.com/questions/26142723
复制相似问题