简体   繁体   中英

how to display files inside directory

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

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));
}

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);
}
foreach (string filename in filePaths) {
  Console.WriteLine(filename);
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM