What is the easiest way to find out the lastest created file in a directory using C#?
I was puzzed few days ago about sorting the lastest created file in the directory. The methods which i thought of were too complicated having to check the file infomation and do some sorting of the files. Later i thought of some simple way and i believe it is very creative method.
//Code to save the file
string saveImageLocation = @"C:\store";
{
string fileName = DateTime.Now.Ticks + new FileInfo(openFileDialog1.FileName).Name;
if (!File.Exists(saveImageLocation + fileName ))
{
File.Copy(openFileDialog1.FileName, saveImageLocation + fileName);
}
}
catch (DirectoryNotFoundException)
{
Directory.CreateDirectory(saveImageLocation);
}
//Code to retrive the image file.
string saveImageLocation = @"C:\store";
pictureBox2.ImageLocation = Directory.GetFiles(saveImageLocation)[Directory.GetFiles(saveImageLocation).Length - 1];
The main idea is naming the creating file by appending the date to the file name. Since date has it own special character, i would suggest using ticks to represent the date.
//Code to save the file
string saveImageLocation = @"C:\store";
{
string fileName = DateTime.Now.Ticks + new FileInfo(openFileDialog1.FileName).Name;
if (!File.Exists(saveImageLocation + fileName ))
{
File.Copy(openFileDialog1.FileName, saveImageLocation + fileName);
}
}
catch (DirectoryNotFoundException)
{
Directory.CreateDirectory(saveImageLocation);
}
//Code to retrive the image file.
string saveImageLocation = @"C:\store";
pictureBox2.ImageLocation = Directory.GetFiles(saveImageLocation)[Directory.GetFiles(saveImageLocation).Length - 1];
The main idea is naming the creating file by appending the date to the file name. Since date has it own special character, i would suggest using ticks to represent the date.
Comments