get most recently created file in a directory
Hi-
I'm not a C# programmer and I need to add some code to a project that determines the most recently created file in a directory.
I'm not familiar with the .Net framework. I'm happy to work on syntax problems and any loops- but can someone sketch out what kinds of calls I should use?
Thanks! Andy
Re: get most recently created file in a directory
I think this should work :
Code:
using System.IO;
public class FileHelper
{
static public string GetMostRecentFile(string sFolderPath)
{
DateTime dateTime;
bool fFirst = true;
string sMostRecentFile = "";
foreach (string sFile in Directory.GetFiles(sFolderPath))
{
if (fFirst)
{
fFirst = false;
dateTime = File.GetCreationTime(Path.Combine(sFolderPath, sFile));
sMostRecentFile = sFile;
}
else
{
DateTime dateTimeFile = File.GetCreationTime(Path.Combine(sFolderPath, sFile));
if (dateTimeFile > dateTime)
{
dateTime = dateTimeFile;
sMostRecentFile = sFile;
}
}
}
return Path.Combine(sFolderPath, sMostRecentFile);
}
}
If not, it should show you how to do it anyway.
Darwen.