DEV Community

Ricardo
Ricardo

Posted on • Originally published at rmauro.dev on

Getting the file name without extension in C# - #TIP

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);
}
Enter fullscreen mode Exit fullscreen mode

Credits go for this answer in StackOverflow forum.

Getting file names without extensions

Top comments (0)