This question already has answers here:
Check last modified date of file in C#
(5 answers)
Closed 3 years ago.
I am writing some code for a program that archives files. So I need to find files which were last modified one year ago.
string[] as_Datien = Directory.GetFiles(s_Pfad, "*.*", SearchOption.AllDirectories);
for (int i_Stelle = 0; i_Stelle < as_Datien.GetLength(0); i_Stelle++)
{
}
I want to check if a file was last modified 1 year ago.
You can try using Linq and FileInfo to get file's last modification date:
DateTime threshold = DateTime.Now.AddYears(-1);
// files which was modified earlier than 1 year ago
string[] as_Datien = Directory
.EnumerateFiles(s_Pfad, "*.*", SearchOption.AllDirectories)
.Where(file => new FileInfo(file).LastWriteTime < threshold)
.ToArray();
You are probably looking for the File.GetLastWriteTime() method.
It returns the date and time of the last edition of files and/or folders.
Check the doc out.
As has already been mentioned use File.GetLastWriteTime() and check that date against today's date one year ago using DateTime.Now.AddYears(-1)
Related
This question already has answers here:
Exact file extension match with GetFiles()?
(8 answers)
Closed 4 years ago.
I need to get all .txt files in all subfolders of a specified folder so I do:
var files = Directory.GetFiles(reportsFolder, "*.txt", SearchOption.AllDirectories);
However, in some folders I also have files with extensions like .txt_TODO, and these are also being retrieved by GetFiles.
How can I skip these files?
Just filter the result by comparing the extension to ".txt", e.g. using LINQ:
var files = Directory.GetFiles(reportsFolder, "*.txt", SearchOption.AllDirectories)
.Where(f => Path.GetExtension(f).Equals(".txt", StringComparison.OrdinalIgnoreCase))
.ToArray();
This question already has answers here:
How to find the most recent file in a directory using .NET, and without looping?
(11 answers)
Closed 7 years ago.
I have this line:
imageList = Directory.GetFiles(#"e:\webbrowserimages\", "*.bmp").ToList();
This is a List
But i want now just one string variable single variable not a list that will contain the last saved image file on the hard disk.
If for example i have 10 images on the hard disk and the first one is: Image0.bmp then Image1.bmp so the string variable will contain Image10.bmp
And then if on my hard disk there are 24 images then the variable string should contain Image24.bmp
You can do this to get the latest bitmap file in the direcotry,
var directory = new DirectoryInfo(#"e:\webbrowserimages\");
var myFile = (from f in directory.GetFiles("*.bmp")
orderby f.LastWriteTime descending
select f).First();
Unless no other parameter is available better not relying on file name in filtering latest image.
This question already has answers here:
Getting Date or Time only from a DateTime Object
(7 answers)
Closed 8 years ago.
I know that, Example: DateTime fileCreatedDate = File.GetCreationTime(#"C:\Example\MyTest.txt");will return the date and time of a folder creation but how do I only get the "Date"Example "2014-12-01" or "Decemeber 12,2014" of the folder creation excluding the time?
You can use the DateTime.Date property for that, i.e. fileCreatedDate.Date.
This question already has answers here:
Sorting Directory.GetFiles()
(13 answers)
Closed 8 years ago.
I am creating the image of the PowerPoint file programmatically. And after saving the Images to the local drive I am getting the files using DirectoryInfo.GetFiles().
I am saving the image files with the sequence numbers.
My Files:
My issue is when I get the files, are not in the sequence I need them in.
The files sequence which I am getting in the FileInfo[] is :
Can any one help me to solve this issue?
The function doesn't make any guarantees about order but you can achieve the desired result with a simple LINQ query;
FileInfo[] sortedFiles = DirectoryInfo.GetFiles().OrderByDescending(x => x.Name).ToArray();
Try this
foreach (FileInfo fi in directory.GetFiles().OrderBy(fi=>fi.FileName))
{
}
This question already has answers here:
Check last modified date of file in C#
(5 answers)
Closed 9 years ago.
How do I read the modify date of the file in C#?
I can read the creation date of the file by using the code below:
ModifyDate = File.GetCreationTime(FilePath)
File.GetLastWriteTime will do the job
I believe you're looking for GetLastWriteTime