tags:

views:

113

answers:

5

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

+1  A: 

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

hipplar
+3  A: 
foreach (string filename in filePaths) {
  Console.WriteLine(filename);
}
Aistina
+4  A: 

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
but it doesnot shows me the folder names e.g i got 5 folder and 1 .jpeg it only displays .jpeg.
tike
Use Directory.GetDirectories to print all the folders as well. I edited the post to show this.
Aaron
GetFiles returns an array of type FileInfo , it should be foreach(FileInfo fi in Directory.GetFiles(@"C:\Dir")) { Console.WriteLine(file.Name);}
RandomNoob
absolute brilliant
tike
@tike: Then you want `GetFileSystemEntries` instead of `GetFiles`.
Dan Tao
@bnkdev: You're mistaken; `Directory.GetFiles` returns a string[]. The `DirectoryInfo.GetFiles` method returns a FileInfo[].
Dan Tao
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
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
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
@dan, oh yeah that's right my bad.
RandomNoob
+2  A: 

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

Giorgi
+1  A: 

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

string[] filePaths = Directory.GetFiles(@"c:\dir");
fot (int i = 0; i < filePaths.Length; ++i) {
    string path = filePaths[i];
    Console.WriteLine(System.IO.Path.GetFileName(path));
}
Dan Tao
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