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

后端 未结 7 1704
闹比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: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;
    

提交回复
热议问题