-1

I got following code to get .pdf file names from temp folder but how do remove extension from this? It seems like test.GetFileNameWithoutExtension("*.pdf") doesn't work. Help please.

            DirectoryInfo test = new DirectoryInfo(@"C:\temp"); 
            FileInfo[] Files = test.GetFiles("*.pdf"); 

            comboBox1.DataSource = Files;
            comboBox1.DisplayMember = "Name";

3 Answers3

2
var fileNames = Files.Select(f => Path.GetFileNameWithoutExtension(f.Name)).ToList();
combobox1.DataSource = fileNames;
Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
1

GetFileNameWithoutExtension() is a method of System.IO.Path:

string[] Files = test.GetFiles("*.pdf")
     .Select(x => x => Path.GetFileNameWithoutExtension(x.FullName))
     .ToArray(); 
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
0

The Path class is your friend:

Use Path.Get​File​Name​Without​Extension

https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getfilenamewithoutextension?view=netframework-4.7.2

Flydog57
  • 6,851
  • 2
  • 17
  • 18