84

When I use the line of code as below , I get an string array containing the entire path of the individual files .

private string[] pdfFiles = Directory.GetFiles("C:\\Documents", "*.pdf");

I would like to know if there is a way to only retrieve the file names in the strings rather than the entire paths.

H H
  • 263,252
  • 30
  • 330
  • 514
HelloWorld_Always
  • 1,555
  • 3
  • 22
  • 32

7 Answers7

170

You can use Path.GetFileName to get the filename from the full path

private string[] pdfFiles = Directory.GetFiles("C:\\Documents", "*.pdf")
                                     .Select(Path.GetFileName)
                                     .ToArray();

EDIT: the solution above uses LINQ, so it requires .NET 3.5 at least. Here's a solution that works on earlier versions:

private string[] pdfFiles = GetFileNames("C:\\Documents", "*.pdf");

private static string[] GetFileNames(string path, string filter)
{
    string[] files = Directory.GetFiles(path, filter);
    for(int i = 0; i < files.Length; i++)
        files[i] = Path.GetFileName(files[i]);
    return files;
}
RonyLoud
  • 2,408
  • 2
  • 20
  • 25
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Hi Thomas , I get an error like this 'System.Array' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) Is there anything else that I need to consider ? – HelloWorld_Always Aug 21 '11 at 18:20
  • 1
    Add `using System.Linq;` at the beginning of the file, and a reference to the `System.Core` assembly if it's not already there. It requires .NET 3.5 or higher, I'll post a solution for .NET 2.0 if you need it – Thomas Levesque Aug 21 '11 at 18:24
  • My target is wince7, so the solution for earlier than .NET 3.5 was useful. – drlolly Jun 29 '22 at 11:47
9

You can use the method Path.GetFileName(yourFileName); (MSDN) to just get the name of the file.

bland
  • 1,968
  • 1
  • 15
  • 22
Abbas
  • 14,186
  • 6
  • 41
  • 72
8

You could use the DirectoryInfo and FileInfo classes.

//GetFiles on DirectoryInfo returns a FileInfo object.
var pdfFiles = new DirectoryInfo("C:\\Documents").GetFiles("*.pdf");

//FileInfo has a Name property that only contains the filename part.
var firstPdfFilename = pdfFiles[0].Name;
Jesper Palm
  • 7,170
  • 31
  • 36
2
string[] fileEntries = Directory.GetFiles(directoryPath);

foreach (var file_name in fileEntries){
    string fileName = file_name.Substring(directoryPath.Length + 1);
    Console.WriteLine(fileName);
}
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
himanshu
  • 21
  • 2
  • 3
    Can you provide an explanation of your answer? Can you explain how your solution is substantially different from Thomas Levesque's 8-year-standing answer? And, in particular, can you explain why you use `Substring` rather than making use of the `Path` function that will do the same, in a potentially more robust way? – Wyck Aug 26 '19 at 19:14
1

There are so many ways :)

1st Way:

string[] folders = Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly);
string jsonString = JsonConvert.SerializeObject(folders);

2nd Way:

string[] folders = new DirectoryInfo(yourPath).GetDirectories().Select(d => d.Name).ToArray();

3rd Way:

string[] folders = 
    new DirectoryInfo(yourPath).GetDirectories().Select(delegate(DirectoryInfo di)
    {
        return di.Name;
    }).ToArray();
makim
  • 3,154
  • 4
  • 30
  • 49
Mshini
  • 71
  • 3
1

You can simply use linq

Directory.EnumerateFiles(LoanFolder).Select(file => Path.GetFileName(file));

Note: EnumeratesFiles is more efficient compared to Directory.GetFiles as you can start enumerating the collection of names before the whole collection is returned.

0
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GetNameOfFiles
{
    public class Program
    {
        static void Main(string[] args)
        {
           string[] fileArray = Directory.GetFiles(@"YOUR PATH");
           for (int i = 0; i < fileArray.Length; i++)
           {

               Console.WriteLine(fileArray[i]);
           }
            Console.ReadLine();
        }
    }
}
Mayur Narula
  • 150
  • 1
  • 4