How to display files inside a directory

I am trying to get a list of files inside a directory in this case "c: \ dir \" (of course, I have files inside), and I wanted to display the name of these files in the console assembly in C # ....

I originally 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 these files ....... any help would be appreciated .....

thank.

(further I would like to know, if possible, any idea of ​​sending these paths to a dynamic html page .... any general concept of how to do this ...)

+3
source share
5 answers

If by "file names" you mean literally only names , not 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));
}
+4
source

:

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

foreach(string file in Directory.GetFiles(@"C:\dir"))
{
    Console.WriteLine(file);
}
+4
foreach (string filename in filePaths) {
  Console.WriteLine(filename);
}
+3

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

+2
source

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

+1
source

Source: https://habr.com/ru/post/1730578/


All Articles