How do I get the latest folder created (Help!!!)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have partial code that works (below) that returns the folders in the
directory to a listbox but it is not sorted by LastCreateDate or
LastWriteDate. I only need to return 1 record (the most recently created
folder to a textbox)


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

Try
ListBox1.Items.Clear()
Dim path As String = "S:\Movies\ABC\FMRSST"

For Each f As String In System.IO.Directory.GetDirectories(path)
Dim filename As String = f.Substring(f.LastIndexOf("\") + 1)
Dim sLastWritedate As String =
System.IO.File.GetLastWriteTime(path)
'Dim fn_withoutextn As String = filename.Substring(0,
filename.IndexOf("."))
ListBox1.Items.Add(filename)
Next
Catch ex As System.Exception
End Try
End Sub


thanks in advance
 
Use the System.IO.Directory.GetCreationTime(path) method to get the creation
time of each directory. Using an integer indexed loop, create a variable
that contains DateTime.MinValue, and compare it to the result of each
Directory. Also create a variable that stores the index in the loop, and
initialize it to 0. When a Directory's Creation Time is greater than the
DateTime variable, set the DateTime variable to the Directory's Creation
Time, and the index variable to the index in the loop. When you've looped
through all Directories, the index variable will contain the index of the
Directory with the latest Creation Time.

--
HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Thanks Kevin but I forgot to mention that I am two weeks into dotnet with not
formal training. I am sorry to mislead you with my code but I found that
online. Could you or anyone provide code as I am under a tight deadline.

Thanks in advance.
 
This is not VB.NET code, I am sending you a snipped of C# code. If you need
the complete project email me

string[] directories = Directory.GetDirectories(textBox1.Text);

DateTime latestModified = DateTime.MinValue;
string latestModifiedDirectory = string.Empty;

foreach (string directory in directories)
{
// Last created
// DateTime directoryTime = Directory.GetCreationTime(directory);

// Last modified
DateTime directoryTime = Directory.GetLastWriteTime(directory);
if (directoryTime > latestModified)
{
latestModified = directoryTime;
latestModifiedDirectory = directory;
}
label1.Text = latestModifiedDirectory;
}


Sincerely,
-Arun
 
Back
Top