Reputation: 67
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?
Upvotes: 2
Views: 327
Reputation: 1503449
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;
Upvotes: 10
Reputation: 935
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;
Upvotes: 3