c# – Getting file names without extensions

c# – Getting file names without extensions

You can use Path.GetFileNameWithoutExtension:

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

Although I am surprised there isnt a way to get this directly from the FileInfo (or at least I cant see it).

Use Path.GetFileNameWithoutExtension().

c# – Getting file names without extensions

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.

Leave a Reply

Your email address will not be published.