简体   繁体   中英

C# File Last Modified time

I am trying to get the last write time of a particular file. This is the code and it works:`

DirectoryInfo DR = new DirectoryInfo(folderPath);  
FileInfo[] FR2 = DR.GetFiles("InputData.csv");
var FileLastModified= null;

foreach (FileInfo F1 in FR2)
{
    FileLastModified = F1.LastWriteTime;
}

FileLastModified gives me the last write time and I am only interested to find the time of this InputData.csv file. The problem is I do not want to use a for loop and need the write time for just one particular file. Is there a better way to write this without the loop?

You don't have to search through a directory to get a FileInfo - you can construct one directly from the full path . It sounds like you just need:

var fileInfo = new FileInfo(Path.Combine(folderPath, "InputData.csv"));
var lastModified = fileInfo.LastWriteTime;

Yes, you can just pass the path to the file you're interested in to a new FileInfo object.

var fileInfo = new FileInfo(pathToFile);
var fileLastModified = fileInfo.LastWriteTime;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM