Finding Most Recently Modified File

  • Thread starter Thread starter mosdef_underground
  • Start date Start date
M

mosdef_underground

How can I use C# to find the most recently modified file in a folder?
As you can probably guess, I am just a beginner, so any help would be
appreciated.

Thanks!
 
Mos Def (I loved "Oh no!"),

Use the DirectoryInfo class. With that, you can get all the files in
the folder through a call to the GetFiles method. This will return an array
of FileInfo instances, which you can check the LastWriteTime property of to
determine the time the file was last modified. From there you can search
the array and get the last file modified (by looping through and doing a
simple comparison).

Hope this helps.
 
Nicholas,

I'm always glad to meet another Mos Def fan!

I've got my code working ok right now, but there are a few errors that
won't let it compile.


DirectoryInfo Direc = new DirectoryInfo(@"U:\xxxx\xxxx");

FileInfo[] FoldersArr = Direc.GetFiles();

if (FoldersArr.Length > 0)
{
DateTime MostRecent = FoldersArr[1].LastWriteTime;
FileInfo RecentFolder;

foreach (FileInfo f in FoldersArr)
{
if (MostRecent.CompareTo(f.LastWriteTime) < 0)
{
MostRecent = f.LastWriteTime;
RecentFolder = f;
}
}
}

MessageBox.Show(RecentFolder.ToString());
I run across an error on the line
MessageBox.Show(RecentFolder.ToString())

It says:

The type or namespace name 'RecentFolder' could not be found (are you
missing a using directive or an assembly reference?)

I guess I'm forgetting something about object-oriented programming or
maybe something easier.

Thanks for your help!
 
OT, but I had to post...

I get Google alerts on a very few things. One is my name, Nicholas. One
is Mos Def. This thread tripped both. Just odd, since I don't get many
for either, especially Mos Def, and to all of a sudden get one for
both...

Sorry for the interuption.


jtnt
http://mosdef.funky4u.com
 
Back
Top