How to get file names from the directory, not the entire path

后端 未结 4 602
别跟我提以往
别跟我提以往 2020-12-09 08:51

I am using the below method to get the file names. But it returns the entire path and I don\'t want to get the entire path. I want only file names, not the entire path.

相关标签:
4条回答
  • 2020-12-09 09:04

    Try GetFileName() method:

    Path.GetFileName(filename);
    
    0 讨论(0)
  • 2020-12-09 09:13

    You could use the GetFileName method to extract only the filename without a path:

    string filenameWithoutPath = Path.GetFileName(filename);
    
    0 讨论(0)
  • 2020-12-09 09:15
    You can use this, it will give you all file's name without Extension
    
        List<string> lstAllFileName = (from itemFile in dir.GetFiles()
                                                   select Path.GetFileNameWithoutExtension(itemFile.FullName)).Cast<string>().ToList();
    
    0 讨论(0)
  • 2020-12-09 09:19

    System.IO.Path is your friend here:

    var filenames = from fullFilename
                    in Directory.EnumerateFiles(targetdirectory,"backup-*.zip")
                    select Path.GetFileName(fullFilename);
    
    foreach (string filename in filenames)
    {
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题