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

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

提交回复
热议问题