从多个位置的多个文件中获取元数据

时间:2012-10-17 18:48:49

标签: vb.net metadata

我正在尝试从多个文件中访问元数据。我已经找到了获取元数据的代码,但我需要能够从不同文件夹中的多个文件中获取。文件类型都是一样的。这可能吗?如果是,可以将其添加到当前代码中吗?

最后,我想获取所有元数据并将其发送到数据库进行比较。

这是我发现的代码,我曾经在一个位置从1个文件中获取它:

Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.IO
Imports Shell32


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Call Main()
End Sub

Sub Main()
    Dim FileName As String
    FileName = "D:\Folder\Folder1\filename.pst"
    Dim Properties As Dictionary(Of Integer, KeyValuePair(Of String, String)) = GetFileProperties(FileName)
    For Each FileProperty As KeyValuePair(Of Integer, KeyValuePair(Of String, String)) In Properties
        ListBox1.Items.Add(FileProperty.Value.Key & ": " & FileProperty.Value.Value)
    Next
End Sub

Public Function GetFileProperties(ByVal FileName As String) As Dictionary(Of Integer, KeyValuePair(Of String, String))
    Dim Shell As New Shell
    Dim Folder As Folder = Shell.[NameSpace](Path.GetDirectoryName(FileName))
    Dim File As FolderItem = Folder.ParseName(Path.GetFileName(FileName))
    Dim Properties As New Dictionary(Of Integer, KeyValuePair(Of String, String))()
    Dim Index As Integer
    Dim Keys As Integer = Folder.GetDetailsOf(File, 0).Count
    For Index = 0 To Keys - 1
        Dim CurrentKey As String = Folder.GetDetailsOf(Nothing, Index)
        Dim CurrentValue As String = Folder.GetDetailsOf(File, Index)
        If CurrentValue <> "" Then
            Properties.Add(Index, New KeyValuePair(Of String, String)(CurrentKey, CurrentValue))
        End If
    Next
    Return Properties
End Function

End Class

1 个答案:

答案 0 :(得分:1)

查看以下代码是否有帮助 -

Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.IO
Imports Shell32


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Call Main()
End Sub

Sub Main()

    Dim dir As New IO.DirectoryInfo("c:\")
    Dim files As IO.FileInfo() = di.GetFiles("*.pst")
    Dim file As IO.FileInfo
    Dim FileName As String

    For Each file In files

        FileName = file.FullName

        Dim Properties As Dictionary(Of Integer, KeyValuePair(Of String, String)) = GetFileProperties(FileName)
        For Each FileProperty As KeyValuePair(Of Integer, KeyValuePair(Of String, String)) In Properties
            ListBox1.Items.Add(FileProperty.Value.Key & ": " & FileProperty.Value.Value)
        Next
    Next
End Sub

Public Function GetFileProperties(ByVal FileName As String) As Dictionary(Of Integer, KeyValuePair(Of String, String))
    Dim Shell As New Shell
    Dim Folder As Folder = Shell.[NameSpace](Path.GetDirectoryName(FileName))
    Dim File As FolderItem = Folder.ParseName(Path.GetFileName(FileName))
    Dim Properties As New Dictionary(Of Integer, KeyValuePair(Of String, String))()
    Dim Index As Integer
    Dim Keys As Integer = Folder.GetDetailsOf(File, 0).Count
    For Index = 0 To Keys - 1
        Dim CurrentKey As String = Folder.GetDetailsOf(Nothing, Index)
        Dim CurrentValue As String = Folder.GetDetailsOf(File, Index)
        If CurrentValue <> "" Then
            Properties.Add(Index, New KeyValuePair(Of String, String)(CurrentKey, CurrentValue))
        End If
    Next
    Return Properties
End Function

End Class

上面的代码将搜索C:\中的所有.pst文件并读取属性。 要允许不同的文件夹,您需要创建目录数组,并根据您的要求从配置文件或任何其他位置读取它们。