4

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...)

tike
  • 548
  • 3
  • 12
  • 22

5 Answers5

6

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));
}
Omidoo
  • 493
  • 1
  • 6
  • 14
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • actually may i know if its possible for me to send those path dynamically towards html file.... i mean the path that i see in my console application... C:\dir\a.jpeg........... C:\dir\home.txt etc. – tike Jan 29 '10 at 20:23
  • So if you wanted to display a message "Directory is empty..." or something like that would you use an if/else statement? If so what would the criteria for if be? I tried "if i == 0" as my if criteria but it's not working. – Matt P Jul 01 '14 at 19:38
  • @MattP: Sounds like you want to check `if (filePaths.Length == 0)` (outside the `for` loop)? – Dan Tao Jul 01 '14 at 20:11
4

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);
}
Aaron
  • 6,988
  • 4
  • 31
  • 48
  • but it doesnot shows me the folder names e.g i got 5 folder and 1 .jpeg it only displays .jpeg. – tike Jan 29 '10 at 20:12
  • Use Directory.GetDirectories to print all the folders as well. I edited the post to show this. – Aaron Jan 29 '10 at 20:14
  • GetFiles returns an array of type FileInfo , it should be foreach(FileInfo fi in Directory.GetFiles(@"C:\Dir")) { Console.WriteLine(file.Name);} – Ta01 Jan 29 '10 at 20:14
  • @tike: Then you want `GetFileSystemEntries` instead of `GetFiles`. – Dan Tao Jan 29 '10 at 20:16
  • @bnkdev: You're mistaken; `Directory.GetFiles` returns a string[]. The `DirectoryInfo.GetFiles` method returns a FileInfo[]. – Dan Tao Jan 29 '10 at 20:18
  • actually may i know if its possible for me to send those path dynamically towards html file.... i mean the path that i see in my console application... C:\dir\a.jpeg........... C:\dir\home.txt etc. – tike Jan 29 '10 at 20:21
  • Send paths towards an HTML file? I'm not sure what you mean by this. Do you want to fill an HTML file with paths? Are you trying to host a web page that lists files on your local drive? – Aaron Jan 29 '10 at 20:29
  • actually this is basic web server development process.. i want to learn how to create a dynamic html page which could take path for files.... for example above filepaths. basically i want to create a html file which could give me link to that particular file which was displayed in my console application. – tike Jan 29 '10 at 20:33
3
foreach (string filename in filePaths) {
  Console.WriteLine(filename);
}
Aistina
  • 12,435
  • 13
  • 69
  • 89
2

Keep in mind that if there are many files in the folder you will have memory problems.
.Net 4.0 contains a fix: C# directory.getfiles memory help

Community
  • 1
  • 1
Giorgi
  • 30,270
  • 13
  • 89
  • 125
1

foreach(FileInfo f in Directory.GetFiles()) Console.Writeline(f.Name)

Larry Hipp
  • 6,205
  • 3
  • 26
  • 31