我是 VB.NET 的新手,我的任务是编写一个小程序来搜索大约 2000 个 Excel 电子表格的目录,并根据该电子表格文件中自定义文档属性的值整理一个列表以显示。鉴于我在教育或贸易方面远不是计算机程序员,这是一次冒险。
我已经开始工作了,结果很好。问题是,它需要一分钟多的时间才能运行。它正在通过 LAN 连接运行。当我在本地运行它(使用大约 300 个文件的“测试”目录)时,它会在大约 4 秒内执行。
我什至不确定合理的执行速度会发生什么,所以我想我会在这里问。
代码如下,如果有人认为更改可能有助于加快速度。
先感谢您!
Private Sub listByPt()
Dim di As New IO.DirectoryInfo(dir_loc)
Dim aryFiles As IO.FileInfo() = di.GetFiles("*" & ext_to_check)
Dim fi As IO.FileInfo
Dim dso As DSOFile.OleDocumentProperties
Dim sfilename As String
Dim sheetInfo As Object
Dim sfileCount As String
Dim ifilesDone As Integer
Dim errorList As New ArrayList()
Dim ErrorFile As Object
Dim ErrorMessage As String
'Initialize progress bar values
ifilesDone = 0
sfileCount = di.GetFiles("*" & ext_to_check).Length
Me.lblHighProgress.Text = sfileCount
Me.lblLowProgress.Text = 0
With Me.progressMain
.Maximum = di.GetFiles("*" & ext_to_check).Length
.Minimum = 0
.Value = 0
End With
'Loop through all files in the search directory
For Each fi In aryFiles
dso = New DSOFile.OleDocumentProperties
sfilename = fi.FullName
Try
dso.Open(sfilename, True)
'grab the PT Initials off of the logsheet
Catch excep As Runtime.InteropServices.COMException
errorList.Add(sfilename)
End Try
Try
sheetInfo = dso.CustomProperties("PTNameChecker").Value
Catch ex As Runtime.InteropServices.COMException
sheetInfo = "NONE"
End Try
'Check to see if the initials on the log sheet
'match those we are searching for
If sheetInfo = lstInitials.SelectedValue Then
Dim logsheet As New LogSheet
logsheet.PTInitials = sheetInfo
logsheet.FileName = sfilename
PTFiles.Add(logsheet)
End If
'update progress bar
Me.progressMain.Increment(1)
ifilesDone = ifilesDone + 1
lblLowProgress.Text = ifilesDone
dso.Close()
Next
lstResults.Items.Clear()
'loop through results in the PTFiles list
'add results to the listbox, removing the path info
For Each showsheet As LogSheet In PTFiles
lstResults.Items.Add(Path.GetFileNameWithoutExtension(showsheet.FileName))
Next
'build error message to display to user
ErrorMessage = ""
For Each ErrorFile In errorList
ErrorMessage += ErrorFile & vbCrLf
Next
MsgBox("The following Log Sheets were unable to be checked" _
& vbCrLf & ErrorMessage)
PTFiles.Clear() 'empty PTFiles for next use
End Sub