M
Mufasa
Is there an easy way to get the oldest file in a directory without having to
go through all the files comparing the file modified time?
TIA - Jeff.
go through all the files comparing the file modified time?
TIA - Jeff.
Mufasa said:Is there an easy way to get the oldest file in a directory without having to
go through all the files comparing the file modified time?
TIA - Jeff.
Family Tree Mike said:Mufasa said:Is there an easy way to get the oldest file in a directory without having
to
go through all the files comparing the file modified time?
TIA - Jeff.
Here is a way to do this. Replace "c:\foo.txt" as needed.
class Program
{
static void Main ( string [ ] args )
{
Process p = new Process ();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/c dir /B /OD > c:\foo.txt";
p.Start ();
p.WaitForExit ();
using (FileStream fs = new FileStream ( @"c:\foo.txt", FileMode.Open ))
{
TextReader tr = new StreamReader ( fs );
string txt = tr.ReadLine ();
Console.Out.WriteLine ( txt ); // txt has oldest filename
}
Console.In.ReadLine ();
}
}
Mufasa said:Is there an easy way to get the oldest file in a directory without having
to go through all the files comparing the file modified time?
Peter Duniho said:Because, after all, if it's worth doing wrong, it's worth doing wrong in a
more .NET-compliant way.
Seriously, yes...sure, you can use cmd.exe to do the sorting. But why?
No one in their right mind would use code that starts a whole new process
and has to parse text output, just to avoid using the built-in .NET
tools. With LINQ, it's now even more concise, but even without it's not
really that bad.
Pete
It is true that it is always better to make use of the built-in
functionality. I simply like to optimize and polish code and that particular
code sample, even if it is not the best in this case is more .NET-friendly
and can show how to redirect process output and the original. It could be
useful in many other process-related scenarios.
This is about sharing ideas and experience, right?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.