-3

How can I enumerate through all properties of the FileInfo object "info"?

I listed their names below commented out, but I can't enumerate through them with a foreach loop or a for loop? Thanks

    string filePathAndName = "c:\temp.txt";

    FileInfo info = new FileInfo(filePathAndName);  
    // attributes, creation time, creation timeutc, directory, directoryname, exists, extension, fullname, isreadonly, lastaccesstime, lastaccesstimeutc, lastwritetime, lastwritetimeutc, length, name
Brad
  • 1
  • Are you talking about the actual file attributes as in what the file system calls file attributes (archive, read-only, etc.), or the `FileInfo` properties? You need to be careful about terminology to avoid ambiguity. – itsme86 May 16 '17 at 17:03
  • The file info properties. The code below worked great, I searched for 2 hours and was not able to find that. Thanks for the reply – Brad May 16 '17 at 17:14

1 Answers1

0
var info = new FileInfo(filePathAndName);

foreach (var propertyInfo in info.GetType().GetProperties())
{
    Console.WriteLine($"{propertyInfo.Name}: {propertyInfo.GetValue(info)}");
}
Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • Thank you for providing a usable answer. Now that I see how your answer works I am able to read the more generic answer above and understand how that applies as well. – Brad May 16 '17 at 17:17