Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

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

I need to display the full file path of the files in that folder.

It should display all the files in the format of 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 only prints the file name, but I also need it to print the full path of the file.

like image 569
Illep Avatar asked Jun 09 '12 17:06

Illep


People also ask

How do I show the full path of a file in Linux?

The best Linux command to get file path is using pwd command. To use this command, type “pwd” into your terminal and press enter. This command will print the current working directory. The output will be the file path.

What is the full path of a folder?

An absolute path refers to the complete details needed to locate a file or folder, starting from the root element and ending with the other subdirectories. Absolute paths are used in websites and operating systems for locating files and folders. An absolute path is also known as an absolute pathname or full path.


2 Answers

Whats wrong with simply printing path variable?

Btw you can iterate files via foreach statement:

foreach(var path in Directory.GetFiles(@"C:\Name\Folder\"))
{
   Console.WriteLine(path); // full path
   Console.WriteLine(System.IO.Path.GetFileName(path)); // file name
}
like image 124
Sergey Berezovskiy Avatar answered Oct 31 '22 02:10

Sergey Berezovskiy


Use the following,

System.IO.Path.GetFullPath(path);
like image 45
Rajesh Subramanian Avatar answered Oct 31 '22 02:10

Rajesh Subramanian