.Net List the Files And Directories’ Names

Code Snippets 4 U

If you like the GUI version then I have created one with .Net. You can download that from Here . It can be installed as any other program. I have signed it with my key which is not verified by Microsoft. So, you may be shown a warning.

using System;
using System.IO;
using System.Linq;

namespace ListFileNamesInADirectory
{
    class Program
    {
        static void Main(string[] args)
        {
            var c = 'y';
            Console.WriteLine("****************\n****************\nIt will list the names of directories and files in a directory.\n****************\n****************\n");
            do
            {
                Console.WriteLine("Enter the location");
                var path = Console.ReadLine();
                if (Directory.Exists(path))
                {
                    var files = Directory.GetFiles(path, "*");
                    var directories = Directory.GetDirectories(path);
                    var filesAndDirectories = files.Concat(directories).ToList();
                    Console.WriteLine("Files and directories names are\n");
                    filesAndDirectories.ForEach(file =>
                    {
                        var splittedFile = file.Split(Path.DirectorySeparatorChar);
                        Console.WriteLine(splittedFile[splittedFile.Length-1]);
                    });
                }

                Console.WriteLine("\nEnter y to list again\n");
                c = (char) Console.Read();
                Console.ReadLine(); // to read the empty line after character read.
            } while (c == 'y' || c == 'Y');
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

fourteen − = nine