When it comes to file operations, extracting a filename from a given path is a very common occurrence. Fetching the filename allows to quickly manipulate the files as a single entity. It also prevents file clobbering from incorrect filenames.
In this tutorial, we will learn all the methods that you can use to extract a filename from a provided path using C#.
Method 1: Using the Path Class
From the System.IO namespace, we have access to the βPathβ class which contains a plethora of useful methods to deal with files and file paths. One that takes our interest is the GetFileName method which allows us to extract the filename from the provided path.
The method syntax is as follows:
The method accepts the path as a string from which we wish to extract the filename and extension.
The method returns the characters after the last directory separator character in the specified path.
The following example shows how to use this function to extract the filename from the path:
using System.IO;
class Program
{
static void Main()
{
string path = @"/var/www/html/index.php";
string fname = Path.GetFileName(path);
Console.WriteLine(fname);
}
}
As you can see from the given example, we use the βGetFileNameβ method from the βPathβ class to get the filename of the specified path. In this case, the previous code should return the filename as well as the extension.
Output:
It is good to keep in mind that this works even when dealing with Windows-based file paths as demonstrated in the following example:
using System.IO;
class Program
{
static void Main()
{
string path = @"C:\inetpub\wwwroot\index.aspx";
string fname = Path.GetFileName(path);
Console.WriteLine(fname);
}
}
Output:
Method 2: String Manipulation
If you want to manually parse the string to extract the filename, you can take advantage of the substring and the last index of methods to extract the filename from a given string.
Take a look at the following example code:
class Program
{
static void Main()
{
string path = @"C:\inetpub\wwwroot\index.aspx";
int index = path.LastIndexOf('\') + 1;
string fname = path.Substring(index);
Console.WriteLine(fname);
}
}
This technique involves two main methods. The first approach involves us finding the last index of the backslash character in the provided path. This is because each backslash character represents a directory, except the last one which denotes the start of the filename.
We do this by adding 1 to the index which gives us the index at the start of the filename.
The last approach involves us using the Substring() method to extract the filename from the path by passing the start position.
When you are dealing with Unix-styled file paths, you need to switch the target separator as shown in the following example:
class Program
{
static void Main()
{
string path = @"/var/www/html/index.html";
int index = path.LastIndexOf('/') + 1;
string fname = path.Substring(index);
Console.WriteLine(fname);
}
}
Method 3: Using the FileInfo Class
The System.IO namespace also has the βFileInfoβ class which contains properties for copying, deleting, moving, and opening the files.
In our case, we are interested in the βNameβ property which enables us to get the name of the file from the provided filename.
using System.IO;
class Program
{
static void Main()
{
string path = @"/var/www/html/index.html";
FileInfo fileInfo = new FileInfo(path);
string fname = fileInfo.Name;
Console.WriteLine(fname);
}
}
This, in a similar manner, gets the name of the file for both Windows and Unix-styled file paths.
Method 4: Using LINQ
The last method that we can use to extract the filename from a given file path is the LINQ function. This method provides a functional and readable code as shown in the following example:
using System.Linq;
class Program
{
static void Main()
{
string path = @"C:\inetpub\wwwroot\index.aspx";
string fname = path.Split('\').Last();
Console.WriteLine(fname);
}
}
In this case, we split the file path into an array of string using the backslash as the delimiter. We then use the Last() extension method from LINQ to get the last element of the array which represents the file name.
Conclusion
In this tutorial, we learned all the methods that you can use with the β.NETβ ecosystem to fetch the filename from a given file path.