This post will discuss how to remove an extension from a file name in C#.

1. Using Path.ChangeExtension() method

To get the full path without the extension, consider using Path.ChangeExtension() method. It takes two parameters – the path to modify and the new extension. The idea is to pass null to the ChangeExtension() method parameter to remove the existing extension.

Download  Run Code

2. Using Path.GetExtension() method

The idea is to get the extension name using the Path.GetExtension() method and remove the same characters from the end of the string as the extension’s length.

Download Code

3. Using Path.GetFileNameWithoutExtension() method

If only the file name is needed without path information and the extension, consider using Path.GetFileNameWithoutExtension() method.

Download  Run Code

That’s all about removing the extension from a file name in C#.