Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Get All file names without extension from directory

Tags:

c#

Im looking for a way to read ALL txt files in a directory path without their extensions into an array. Ive looked through the path.getFileNameWithoutExtension but that only returns one file. I want all the *.txt file names from a path i specify

THanks

like image 677
hWorld Avatar asked Jun 13 '11 20:06

hWorld


2 Answers

Directory.GetFiles(myPath, "*.txt")
    .Select(Path.GetFileNameWithoutExtension)
    .Select(p => p.Substring(1)) //per comment
like image 87
Yuriy Faktorovich Avatar answered Sep 16 '22 16:09

Yuriy Faktorovich


Something like:

String[] fileNamesWithoutExtention = 
Directory.GetFiles(@"C:\", "*.txt")
.Select(fileName => Path.GetFileNameWithoutExtension(fileName))
.ToArray();

Should do the trick.

like image 24
DaveShaw Avatar answered Sep 19 '22 16:09

DaveShaw