473,408 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Finding Most Recently Modified File

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
3 6009
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Sean Ross | last post by:
Hi. Recently I made a small script to do some file transferring (among other things). I wanted to monitor the progress of the file transfer, so I needed to know the size of the files I was...
6
by: Ajay | last post by:
hi! i am reading a file on the Web. How can i find out when it was last modified? thanks
13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
2
by: Steve Long | last post by:
Hey, does anybody know where VS.NET stores it's most recently used list that it displays on the Start page when you first open up VS? I would like to kick a item off of there but don't know...
2
by: Robizzle | last post by:
Sorry, I must be blind but I can't seem to find it anywhere. What function will return the time a file was created? I see filemtime(...) returns the time that file was last Modified and...
2
by: Dyl | last post by:
Hello, I'm working on trying to find the most recently modified file, but I keep coming across an error when building. Here's my code: DirectoryInfo Direc = new...
3
by: Joe Kimbler | last post by:
I'm writing a "Hot Directory" software package that watches for an MP3 file to be uploaded to an FTP directory. When the file has completed uploading, it will be converted into a WAV file and...
5
by: nospam | last post by:
Hi there, I'm using VBScript to display a list of the ten most recently updated pages on my web site. Right now, the script lists the filenames and the date modified in a given directory. ...
102
by: hug | last post by:
www.webmaster, was suggested that this ng could be a better place.] I've updated my test server to handle if-modified-since. I've noticed that the (old copies I run of) IE and Netscape seem...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.