List of all files in the folder - full path to the file

I have a folder in C:\Name\Folder\ and I have several files.

I need to display the full path to the files in this folder.

It should display all files in the format C:\Name\Folder\file.txt . My code is as follows:

 string[] filePaths = Directory.GetFiles(@"C:\Name\Folder\"); for (int i = 0; i < filePaths.Length; ++i) { string path = filePaths[i]; Console.WriteLine(System.IO.Path.GetFileName(path)); } 

It prints only the file name, but I also need to print the full path to the file.

+6
source share
4 answers

What is not so simple when printing the path variable?

Btw you can iterate over files using the foreach :

 foreach(var path in Directory.GetFiles(@"C:\Name\Folder\")) { Console.WriteLine(path); // full path Console.WriteLine(System.IO.Path.GetFileName(path)); // file name } 
+11
source

Use the following

 System.IO.Path.GetFullPath(path); 
+4
source

Directory.GetFiles returns full paths. You only see the file name because you are calling Path.GetFileName . Just use path if you need the full path.

+4
source

If you already have a path in filePaths, why do you need a call to GetFileName?
In any case, for such information it would be easier to use the Directory / FileInfo classes, which have all the necessary properties (such as Fullpath)

+3
source

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


All Articles