Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display files inside directory

Tags:

c#

directory

list

i am trying to get list of files inside the directory in this case "c:\dir\" (ofcourse i have files inside) and i wanted to display the name of those files in console program build in c#....

initially i did this....

static class Program
    {
        static void Main()
        {
            string[] filePaths = Directory.GetFiles(@"c:\dir\");
            Console.WriteLine();
            Console.Read();


        }
    }

how can i see the name of those files....... any help would be appreciated.....

thank you.

(further i would like to know if possible any idea on sending those file path towards dynamic html page.... any general concept how to do that...)

like image 990
tike Avatar asked Dec 05 '22 03:12

tike


2 Answers

If by "file names" you mean literally just the names and not the full paths:

string[] filePaths = Directory.GetFiles(@"c:\dir");
for (int i = 0; i < filePaths.Length; ++i) {
    string path = filePaths[i];
    Console.WriteLine(System.IO.Path.GetFileName(path));
}
like image 112
Dan Tao Avatar answered Dec 28 '22 22:12

Dan Tao


Loop through the files and print them one at a time:

foreach(string folder in Directory.GetDirectories(@"C:\dir"))
{
    Console.WriteLine(folder);
}

foreach(string file in Directory.GetFiles(@"C:\dir"))
{
    Console.WriteLine(file);
}
like image 23
Aaron Avatar answered Dec 29 '22 00:12

Aaron