You want to test file names to see if they have a specific extension. Your program may only need to test for one file extension, and you question if the Path.GetExtension method is ideal. Here we see how you can use this method and also compare its performance to a simpler but custom method, using the C# programming language.
~~~ Key points: ~~~
You can use Path.GetExtension to test extensions.
The method includes the separator character ".".Here we look at the functionality of the Path.GetExtension method in the C# language, which is found in the base class library. This sample code simply extracts the extension from the string literal. The resulting extension includes the separator character, which is a dot.
=== Program that uses Path.GetExtension method (C#) ===
using System;
using System.IO;
class Program
{
static void Main()
{
string p = @"C:\Users\Sam\Documents\Test.txt";
string e = Path.GetExtension(p);
if (e == ".txt")
{
Console.WriteLine(e);
}
}
}
=== Output of the program ===
.txtNotes. This code isn't clear in several ways: first, you have to know that the extension will have the dot at the start. Next, you need to know whether the extension will be case sensitive—it is.
Here we look at some implementation issues. Most developers will first turn to Path.GetExtension, but I found that it can cause code to become more complicated than other methods. In Reflector I found that Path.GetExtension does quite a lot without telling us.
Path.GetExtension checks the entire path for invalid chars. This step is redundant if you already know your path is valid, such as when you received it from Directory.GetFiles. It looks for a separator char. The implementation checks for DirectorySeparatorChar, AltDirectorySeparatorChar, and VolumeSeparatorChar. [This article is based on .NET Framework 3.5 SP1]
Changing extensions. This site also contains information about how you can change the extensions on path representations in memory. The .NET Framework provides the Path.ChangeExtension method for this purpose, which is useful for changing paths but does not actually rename the files in any permanent way.
(See Path.ChangeExtension Method.)
Here we see an alternative method to check the file extensions. Often you can rewrite the framework methods so that your code does far less internally, and is also clearer to read. You can pull all the logic into a static helper method. This makes the code clearer and faster.
=== Program that uses custom path method (C#) ===
using System;
class Program
{
static void Main()
{
string p = @"C:\Users\Sam\Documents\Test.txt";
if (IsTxtFile(p))
{
Console.WriteLine(".txt");
}
}
static bool IsTxtFile(string f)
{
return f != null &&
f.EndsWith(".txt", StringComparison.Ordinal);
}
}
=== Output of the program ===
.txtDescription. It tests the path for null. You cannot use an instance method, such as EndsWith, without an actual instance, so testing for null avoids exceptions here. It calls EndsWidth. Your file extension is at the end of the path, so calling EndsWith is a clear and fast way to do it. What I like the most about this example is that it has a more natural-language and simpler interface.
+++ File extension checking method benchmark +++
Based on .NET 3.5 SP1.
Using custom method was much faster than Path class.
First set shows True result.
Second set shows False result.
Path.GetExtension: 1322 ms
Custom IsTxtFile method: 326 ms [faster]
Path.GetExtension: 1296 ms
Custom IsTxtFile method: 208 ms [faster]Here we looked at different ways you can check file extensions in the C# programming language. The custom method here is more expressive to me, and also involves less file system code. It separates the implementation from the expression you want to test. There is some validity to always preferring the .NET framework Path classes, but this alternative method here has merit with expressiveness and performance.