These days I was asking myself how to get the file name without the extension. I confess I was tempted to use a regex to solve this problem :)

But no worries. There is a method in System.IO.Path specific for this situation.

using System.IO;

///
/// Get file name without extension
///
static string GetFileName(string path)
{
    return Path.GetFileNameWithoutExtension(path);
}

///
/// Get file name without extension
///
static string GetFileName(FileInfo fileInfo)
{
    return Path.GetFileNameWithoutExtension(fileInfo.Name);
}

Credits go for this answer in StackOverflow forum.

Getting file names without extensions
When getting file names in a certain folder: DirectoryInfo di = new DirectoryInfo(currentDirName);FileInfo[] smFiles = di.GetFiles(”*.txt”);foreach (FileInfo fi in smFiles){ builder.Append(fi.