c# - Getting file names without extensions

ID : 10221

viewed : 17

Tags : c#.netc#

Top 5 Answer for c# - Getting file names without extensions

vote vote

91

You can use Path.GetFileNameWithoutExtension:

foreach (FileInfo fi in smFiles) {     builder.Append(Path.GetFileNameWithoutExtension(fi.Name));     builder.Append(", "); } 

Although I am surprised there isn't a way to get this directly from the FileInfo (or at least I can't see it).

vote vote

80

This solution also prevents the addition of a trailing comma.

var filenames = String.Join(                     ", ",                     Directory.GetFiles(@"c:\", "*.txt")                        .Select(filename =>                             Path.GetFileNameWithoutExtension(filename))); 

I dislike the DirectoryInfo, FileInfo for this scenario.

DirectoryInfo and FileInfo collect more data about the folder and the files than is needed so they take more time and memory than necessary.

vote vote

60

Path.GetFileNameWithoutExtension(file); 

This returns the file name only without the extension type. You can also change it so you get both name and the type of file

 Path.GetFileName(FileName); 

source:https://msdn.microsoft.com/en-us/library/system.io.path(v=vs.110).aspx

vote vote

57

Use Path.GetFileNameWithoutExtension. Path is in System.IO namespace.

Top 3 video Explaining c# - Getting file names without extensions

Related QUESTION?