Get info from files in a directory in C#

ddanbe 0 Tallied Votes 188 Views Share

If you ever want to get information about files stored in a particular map or directory, you can find out how it is done here. With a little modification you could turn this snippet into a DOS DIR-command or a UNIX(LINUX?) ls-command.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;    //for file functions

namespace DirCommand
    class Program
        static void Main(string[] args)
            // get info for the c: directory or specify another
            DirectoryInfo dirInfo = new DirectoryInfo(@"c:\");
            FileInfo[] files = dirInfo.GetFiles();  // get a list of fileInfo

            // dump it to the console
            Console.WriteLine("Name:\t\tSize:\t\tCreation date:");
            foreach (FileInfo file in files)
                Console.WriteLine("\t" + file.CreationTime.ToString());
                // add any method of FileInfo you like here