C# Path Examples
You need ways to deal with filenames and paths in Windows in your C# programs. Windows uses different directory separators than other platforms. Additionally, you have to escape the \ character as \\, making the syntax harder to understand. Here we see how to use the excellent Path class in the .NET Framework and the C# programming language.
Input: cat.aspx
GetFileName: cat.aspx
GetFileNameWithoutExtension: cat
GetDirectoryName: -
Input: really-long-page.aspx
GetFileName: really-long-page.aspx
GetFileNameWithoutExtension: really-long-page
GetDirectoryName: -
Input: test.aspx
GetFileName: test.aspx
GetFileNameWithoutExtension: test
GetDirectoryName: -
Input: invalid-page
GetFileName: invalid-page
GetFileNameWithoutExtension: invalid-page
GetDirectoryName: -
Input: Content/Rat.aspx
GetFileName: Rat.aspx
GetFileNameWithoutExtension: Rat
GetDirectoryName: Content
Input: http://dotnetperls.com/Cat/Mouse.aspx
GetFileName: Mouse.aspx
GetFileNameWithoutExtension: Mouse
GetDirectoryName: http:\dotnetperls.com\Cat
Input: C:\Windows\File.txt
GetFileName: File.txt
GetFileNameWithoutExtension: File
GetDirectoryName: C:\Windows
Input: C:\Word-2007.docx
GetFileName: Word-2007.docx
GetFileNameWithoutExtension: Word-2007
GetDirectoryName: C:\
Path example
You will often need to extract parts of filename paths in your programs. The .NEW framework team at Microsoft has thought of this problem already and the Path class is ideal for our use. You can access it by adding "using System.IO;" at the top of your class. First here we see a short console program that shows four Path methods.
Program that uses Path methods [C#]
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "C:\\stagelist.txt";
string extension = Path.GetExtension(path);
string filename = Path.GetFileName(path);
string filenameNoExtension = Path.GetFileNameWithoutExtension(path);
string root = Path.GetPathRoot(path);
Console.WriteLine("{0}\n{1}\n{2}\n{3}",
extension,
filename,
filenameNoExtension,
root);
}
}
Output
.txt
stagelist.txt
stagelist
C:\Description. First, the System.IO namespace is included. The extension of the file, the actual filename and the filename without the extension, and then the path root are taken. Note that the path root will always be "C"\\", with the trailing separator, even when the file is nested in many folders.
Get filename
You can get the filename alone by calling the Path.GetFileName method. This will return the filename at the end of the path, along with the extension, such as .doc or .exe. There is also a method to just get the extension, and one just to get the name with no extension. That method is called Path.GetFileNameWithoutExtension.
Output description
Near the top of this article, there is a table containing the output of three Path methods on a variety of input. The results are from the methods Path.GetFileName(), Path.GetFileNameWithoutExtension(), and Path.GetDirectoryName(). Next we see some examples and discussion.
Generating the example output. The next code example shows how you can see the results that are shown. Path has static methods, which means you don't ever need to write "new Path()". Instead, you can simply type Path.GetFileName(), for example. Here is the C# console program you can use.
Program that tests Path class [C#]
using System;
using System.IO;
class Program
{
static void Main()
{
string[] pages = new string[]
{
"cat.aspx",
"really-long-page.aspx",
"test.aspx",
"invalid-page",
"something-else.aspx",
"Content/Rat.aspx",
"http://dotnetperls.com/Cat/Mouse.aspx",
"C:\\Windows\\File.txt",
"C:\\Word-2007.docx"
};
foreach (string page in pages)
{
string name = Path.GetFileName(page);
string nameKey = Path.GetFileNameWithoutExtension(page);
string directory = Path.GetDirectoryName(page);
//
// Display the Path strings we extracted.
//
Console.WriteLine("{0}, {1}, {2}, {3}", page, name, nameKey, directory);
}
}
}
Output
(See table at top of article.)Note on extensions. Path.GetFileNameWithoutExtension will return the entire file name if there's no extension on the file.Path.GetDirectoryName returns the entire string except the file name and the slash before it.
Path methods and URLs. Look at the table above where the directory name of the URL is received. The slashes are reversed into Windows-style slashes. This is not desirable with virtual paths or URLs. You should not use Path methods on URIs; instead, use the Uri class.
Uri ClassNote. The drive letter such as C:\ is part of the directory name. Windows directories include the drive letter along with the entire folder name. Again, they don't include the trailing slash \.
Extensions
Extensions include the period (.) before the three letters. So, in the above array, the extensions would be: ".aspx" ".txt" or ".docx". To get the extension of the file from the path, use Path.GetExtension(value).
Path.GetExtension (File Extension)Path.Combine
Path.Combine is a useful method, but there are edge cases it cannot solve. It can't figure out what you want if what it receives is confusing. However, different inputs can yield the same result path. Here's a screenshot where we combine the folder "Content\\" with the file name "file.txt".