Click here to Skip to main content
15,891,738 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been working with a software program from hp called openview and it delivers and installs software on all our computers. It creates these logs each time it runs a software connect and I have to open and look through and im tired of having to search for it and then open it.

now i have deen using the below code to get all the files that have *_mach.log. I need help going from that list and open the one that has the highest number in front of it. so a normal log file looks like this 20120516_mach.log, and the numbers you see are the date it was created which also lets us know when a software connect last ran.

i have been using the below code to just get the file names and what i really want to do is once it has the most current log is to open it with notepad. now the below code i have going to a list view for right now and i don't want to use it in the end result. What i want is when you click the button to open the log it opens the most current file in notepad.

code for searching the folder:

VB
Dim di As New IO.DirectoryInfo("C:\Users\Zachary\Desktop\Log")
Dim diar1 As IO.FileInfo() = di.GetFiles("*_mach.log")
Dim dra As IO.FileInfo

'list the names of all files in the specified directory
For Each dra In diar1
    ListBox1.Items.Add(dra)
Next



the code i m using to open files with notepad:(example)
Process.Start("notepad.exe", "\\" & strcomputer & "\C$\Windows\System32\drivers\etc\HOSTS")



help would be great...
Posted
Comments
Sergey Alexandrovich Kryukov 25-May-12 16:06pm    
What about WinFomrs? This is irrelevant. Better remove this tag, add ".NET".
Why would you use Notepad, ever, if with Forms you can use TextBox?
--SA

1 solution

Probably you mean "most recent file". Most recently created or most recently written?

After you get all files in question in the form of the instance of System.IO.FileInfo, you can use System.IO.CreationTime or System.IO.LastWriteTime (depending on what time do you need) and compare time. Please see:
http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx[^].

Remember that the time resolution is not high, it's a millisecond. Please see:
http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/bed97747-923d-4078-9ae3-04e0b0192342[^].

By the way, many are confused about how to compare the instances of System.DateTime. The answer is simple: use the operator "<" or ">". Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900