0

I am looking to do something like the pseudocode below without knowing what properties are available beforehand:

foreach(var property in folder){
     var propertyName = property.Name;
     var propertyValue = property.Value;

     //do other stuff with the values.
}

I would like to be able to do the same with a file (instead of a folder) as well.

Thanks in advance.

Matt Cashatt
  • 20,878
  • 24
  • 73
  • 105
  • @TimS.--Seriously? How about something constructive that the community can use. – Matt Cashatt Jan 21 '14 at 15:59
  • 2
    [How to get the list of properties of a class?](http://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class) – Tim S. Jan 21 '14 at 16:02
  • @TimS. I'm not sure how that's relevant. OP is taking about attributes of files / folders on the file system, not reflection. – vcsjones Jan 21 '14 at 16:17
  • Well, either he will have to use the documentation and find what properties are defined for those File/Folder objects (what they are we don't know, as he's giving no indication of what he's using), or either he's going to have to use reflection. So I think TimS' comment is not so off-topic. – Luc Morin Jan 21 '14 at 16:27
  • What kind of "Properties" are you referring to? Most attributes are just boolean values (Compressed, Encrypted, Read-Only, etc) – vcsjones Jan 21 '14 at 16:29
  • Are you trying to [iterate](http://stackoverflow.com/questions/4171140/iterate-over-values-in-flags-enum) over the list of [`Attributes`](http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.attributes(v=vs.110).aspx)? – Tim S. Jan 21 '14 at 16:40
  • @TimS.--Yes, I believe that is probably the better term. I am not sure what the exhaustive list of attributes would be, but if it is present, I want to grab it from code. Things like creation date, last access date, author (if available), etc. Thanks for your help. – Matt Cashatt Jan 21 '14 at 16:47

3 Answers3

1

try DirectoryInfo.GetFiles(pattern). It returns a FileInfo[] array with some attributes like name, file attributes, creation date,...

mnieto
  • 3,290
  • 3
  • 17
  • 35
1

Assuming you are talking about the kinds of attributes such as "Read Only", "Hidden", etc, you could do something like this to build a dictionary:

var dir = new DirectoryInfo(@"C:\temp");
var attributes = new Dictionary<string, bool>();
foreach (int value in Enum.GetValues(typeof (FileAttributes)))
{
    var attribute = (FileAttributes)value;
    var hasAttribute = dir.Attributes.HasFlag(attribute);
    attributes.Add(attribute.ToString(), hasAttribute);
}

The attributes dictionary will contain the possible attributes for the directory, and a boolean value to indicate if that attribute is present or not.

You can do the exact same thing with a FileInfo for files instead of a DirectoryInfo.

vcsjones
  • 128,004
  • 28
  • 283
  • 274
0

In the end, @TimS's suggestion of following this link worked the best for me. Here is the complete code:

        var fileInfo = new FileInfo(folderPath);
        var folderProperties = fileInfo.GetType().GetProperties();
        foreach (var property in folderProperties)
        {
                var propName = property.Name
                var propValue = property.GetValue(fileInfo, null);

                //do stuff                                         
        }
Community
  • 1
  • 1
Matt Cashatt
  • 20,878
  • 24
  • 73
  • 105