Get info from files in a directory in C#

ddanbe 0 Tallied Votes 217 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.Write(file.Name.PadRight(16));
                Console.Write(file.Length.ToString().PadRight(16));
                Console.WriteLine("\t" + file.CreationTime.ToString());
                // add any method of FileInfo you like here
            }
            Console.ReadKey();
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.