AMeh
AMeh

Reputation: 211

How to get file information within directory in c#

I want to get the file information within directory in MVC application.

Below is my code which gets me file names but I also want the file created dates and file size,

if (Directory.Exists(path))
{
    files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
                                     .Select(Path.GetFileName).ToArray();
}

How do I do that?

Upvotes: 0

Views: 2051

Answers (3)

KiwiPiet
KiwiPiet

Reputation: 1334

var files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
            .Select(f => new FileInfo(f)).ToArray();

Upvotes: 1

Lukas Kabrt
Lukas Kabrt

Reputation: 5489

There isn't any method that provides the data you require for all files in the directory.

You need to get this information for every file separately

foreach(var filename in file) {
   var fileInfo = new FileInfo(filename);
   var fileSize = fileInfo.Length;
   var fileCreated = fileInfo.CreationTime;
}

Upvotes: 2

Pheonyx
Pheonyx

Reputation: 851

You will most likely need to iterate around for files list and use the FileInfo class to get said info.

Details on this class can be found here

Basically use a for/foreach loop and for each file name query the properties of an instantiated FileInfo class for things such as created date.

Upvotes: 0

Related Questions