简体   繁体   中英

How to Get and Use File Name from FileInfo

So basically what I'm trying to do is get the date modified of a file that FileInfo gets. Hopefully the code will explain a little better.

DirectoryInfo prefetch = new DirectoryInfo("c:\\Windows\\Prefetch");
FileInfo[] log = prefetch.GetFiles("2*");
if (log.Length == 0)
    MessageBox.Show("Nothing Found");
else
    DateTime modified = System.IO.File.GetLastWriteTime(Convert.ToString(log));
    MessageBox.Show(Convert.ToString(modified));

The above code does not work/do what I want it do. sSo say in Prefetch there is a file called log2.txt. the FileInfo would detect that it is there, but I'm trying to display the Date Modified of that file. I know how to see if it exists and how to show the date modified, but I can't figure out how to retrieve the name of the file/file that FileInfo finds then display the date modified. Any suggestions or answers would be greatly appreciated.

如果只有一个文件,我相信它将是file[0].LastWriteTime

You can check FileInfo class at System.IO.FileSystemInfo.FileInfo

To retrieve the file name and modified date you just access to property Name and LastWriteTime

I modified your example code to show the file name of the first element in your log array.

DirectoryInfo prefetch = new DirectoryInfo("c:\\Windows\\Prefetch");
FileInfo[] log = prefetch.GetFiles("2*");
if (log.Length == 0)
    MessageBox.Show("Nothing Found");
else
   MessageBox.Show(log[0].Name);

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