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

Vb.net 从多个位置的多个文件中获取元数据,vb.net,metadata,Vb.net,Metadata,我正在尝试从多个文件访问元数据。我已经找到了获取元数据的代码,但我需要能够从不同文件夹中的多个文件中获取。文件类型都是相同的。这可能吗?如果是,是否可以将其添加到当前代码中 最后,我想获取所有元数据并将其发送到数据库进行比较 这是我从一个位置的1个文件中找到的代码: Imports System Imports System.Collections.Generic Imports System.Windows.Forms Imports System.IO Imports Shell32 P

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

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

这是我从一个位置的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
导入系统
导入System.Collections.Generic
导入System.Windows.Forms
导入System.IO
进口贝壳32
公开课表格1
私有子表单1_Load(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
调用Main()
端接头
副标题()
将文件名设置为字符串
FileName=“D:\Folder\Folder1\FileName.pst”
Dim属性作为字典(整数的,KeyValuePair(字符串的,字符串的))=GetFileProperties(文件名)
对于每个FileProperty,将其作为属性中的KeyValuePair(整型的,KeyValuePair(字符串型的,字符串型))使用
ListBox1.Items.Add(FileProperty.Value.Key&“:”&FileProperty.Value.Value)
下一个
端接头
公共函数GetFileProperties(ByVal文件名为字符串)作为字典(整型,KeyValuePair(字符串,字符串))
暗壳为新壳
Dim Folder As Folder=Shell。[名称空间](Path.GetDirectoryName(文件名))
作为FolderItem=Folder.ParseName(Path.GetFileName(FileName))的Dim文件
将属性设置为新字典(整数的、键值对的(字符串的、字符串的))()
将索引设置为整数
将密钥设置为整数=Folder.GetDetailsOf(文件,0).Count
对于索引=0到键-1
Dim CurrentKey As String=Folder.GetDetailsOf(无,索引)
Dim CurrentValue为String=Folder.GetDetailsOf(文件,索引)
如果当前值为“”,则
添加(索引,新的KeyValuePair(字符串的,字符串的)(CurrentKey,CurrentValue))
如果结束
下一个
返回属性
端函数
末级

查看下面的代码是否有帮助-

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
导入系统
导入System.Collections.Generic
导入System.Windows.Forms
导入System.IO
进口贝壳32
公开课表格1
私有子表单1_Load(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
调用Main()
端接头
副标题()
Dim dir作为新的IO.DirectoryInfo(“c:\”)
Dim文件作为IO.FileInfo()=di.GetFiles(“*.pst”)
将文件设置为IO.FileInfo
将文件名设置为字符串
对于文件中的每个文件
FileName=file.FullName
Dim属性作为字典(整数的,KeyValuePair(字符串的,字符串的))=GetFileProperties(文件名)
对于每个FileProperty,将其作为属性中的KeyValuePair(整型的,KeyValuePair(字符串型的,字符串型))使用
ListBox1.Items.Add(FileProperty.Value.Key&“:”&FileProperty.Value.Value)
下一个
下一个
端接头
公共函数GetFileProperties(ByVal文件名为字符串)作为字典(整型,KeyValuePair(字符串,字符串))
暗壳为新壳
Dim Folder As Folder=Shell。[名称空间](Path.GetDirectoryName(文件名))
作为FolderItem=Folder.ParseName(Path.GetFileName(FileName))的Dim文件
将属性设置为新字典(整数的、键值对的(字符串的、字符串的))()
将索引设置为整数
将密钥设置为整数=Folder.GetDetailsOf(文件,0).Count
对于索引=0到键-1
Dim CurrentKey As String=Folder.GetDetailsOf(无,索引)
Dim CurrentValue为String=Folder.GetDetailsOf(文件,索引)
如果当前值为“”,则
添加(索引,新的KeyValuePair(字符串的,字符串的)(CurrentKey,CurrentValue))
如果结束
下一个
返回属性
端函数
末级
上面的代码将搜索C:\中的所有.pst文件并读取属性。
要允许不同的文件夹,您需要创建目录数组,并根据需要从配置文件或任何其他位置读取它们。

在示例中,文件路径是硬编码的。您需要获取列表或字符串数组中的文件并对其进行迭代。谢谢您-这似乎很有帮助!我能够从1个文件夹中的文件中获得所需的数据。现在开始访问多个文件夹!