By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,688 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,688 IT Pros & Developers. It's quick & easy.

Finding Most Recently Modified File

P: n/a
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!

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<mo****************@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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!

Nov 17 '05 #2

P: n/a
Dyl
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!

Nov 17 '05 #3

P: n/a
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

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.