How to get only filenames within a directory using c#?

后端 未结 7 1678
闹比i
闹比i 2020-12-04 15:05

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


        
相关标签:
7条回答
  • 2020-12-04 15:31

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

    0 讨论(0)
  • 2020-12-04 15:34

    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;
    }
    
    0 讨论(0)
  • 2020-12-04 15:36

    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 讨论(0)
  • 2020-12-04 15:37

    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();
    
    0 讨论(0)
  • 2020-12-04 15:42
    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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 15:48

    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;
    
    0 讨论(0)
提交回复
热议问题