我是visual basic的新手,我正在尝试制作一个在目录中搜索PDF文件的控制台应用程序,然后打开最近创建的文件。我希望搜索从程序打开到关闭时自动运行。
到目前为止,我已经能够搜索文件夹并打开文件,但我不确定如何使搜索连续进行。
Imports System.IO
Imports System.Windows.Forms
Imports System.Threading
Imports System.Diagnostics.Process
Module Module1
Dim SearchDir As String
Dim OldestFile As FileInfo = Nothing
Dim NewestFile As FileInfo = Nothing
Dim OpenFile As String = Nothing
Dim FileCount As Integer = 0
Sub Main()
SearchDir = "C:\Reports"
Dim Directory As New DirectoryInfo(SearchDir)
Console.WriteLine("Searching {0}", SearchDir)
Console.WriteLine()
Try
For Each finfo In Directory.EnumerateFiles("*.pdf", IO.SearchOption.TopDirectoryOnly)
If OldestFile Is Nothing OrElse finfo.LastWriteTime <= OldestFile.LastWriteTime Then
OldestFile = finfo
End If
If NewestFile Is Nothing OrElse finfo.LastWriteTime >= NewestFile.LastWriteTime Then
NewestFile = finfo
End If
FileCount += 1
Next
Console.WriteLine("Oldest file is {0} {1}.", OldestFile.LastWriteTime, OldestFile.Name)
Console.WriteLine("Newest file is {0} {1}.", NewestFile.LastWriteTime, NewestFile.Name)
Console.WriteLine()
Console.WriteLine("{0} files searched", FileCount)
Try
Dim APP = Shell("C:\Program Files (x86)\Adobe\Reader 11.0\Reader\acrord32.exe", vbMaximizedFocus)
Thread.Sleep(5000) 'Delay 5 seconds for program initialize
Dim reader As Process
reader = Process.Start(NewestFile.FullName)
Thread.Sleep(2000) 'Delay 2 seconds for program initialize
SendKeys.SendWait("^l")
Thread.Sleep(2000) 'Delay 2 seconds for program initialize
SendKeys.SendWait("^+h")
Catch ex2 As Exception
Console.WriteLine("ERROR Activating Adobe Reader.")
Console.ReadLine()
End Try