2012-10-17 60 views
2

我試圖從多個文件中訪問元數據。我已經有了用於獲取元數據的代碼,但我需要能夠從不同文件夾中的多個文件中獲取。文件類型都是一樣的。這可能嗎?如果是這樣,它可以被添加到當前的代碼?從多個位置的多個文件中獲取元數據

事實上,我想抓取所有元數據並將其發送到數據庫進行比較。

這是我發現我以前從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 
+0

在你的示例文件路徑是硬編碼的。您需要在List或字符串數​​組中獲取文件並對其進行迭代。 –

回答

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文件並讀取屬性。 要允許不同的文件夾,您需要創建目錄陣列並根據您的要求從配置文件或任何其他位置讀取它們。

+0

謝謝 - 這似乎有幫助!我能夠從我在1個文件夾中的文件中獲取所需的數據。現在開始訪問多個文件夾! –

+0

最受歡迎的哥們:) –