File Path

Path handles file path processing. The .NET Framework provides effective ways of dealing with filenames and paths. It introduces the Path type in the System.IO namespace. There are complications when dealing with paths. They involve invalid paths and virtual and physical paths.
This article uses the C# Path type. Path handles file locations in a consistent way. It resolves common problems.

You will often need to extract parts of filename paths in your programs. The .NET 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. As an introduction, 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.
GetFileName. 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.
It is useful to see the results of the Path methods on various inputs. Sometimes the methods handle invalid characters as you might expect, but not always. This program calls three Path methods on an array of possible inputs. It prints the results.
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
(Text was reformatted for clarity.)
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:\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.
Note: 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 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 ExtensionPath.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".

Description. The screenshot is of what values Path.Combine produced. It shows that the following two lines of code produce the same result. The Path.Combine method handles certain cases where you have directory separators in different positions.
Program that uses Path.Combine [C#]
using System;
class Program
{
static void Main()
{
//
// Combine two path parts.
//
string path1 = System.IO.Path.Combine("Content", "file.txt");
Console.WriteLine(path1);
//
// Same as above but with a trailing separator.
//
string path2 = System.IO.Path.Combine("Content\\", "file.txt");
Console.WriteLine(path2);
}
}
Output
Content\file.txt
Content\file.txtSyntax hint: The example above also shows how you can refer the Path class by specifying "System.IO.Path" instead of including the namespace at the top of your file. This may be useful in source files that are not file-IO oriented.
When using a C-style language such as C# or C++, you have to add the char \ to your C# code, you must use \\ (two backslashes). That's because C# uses the backslash to escape characters, so you must escape it also. You can see how the slashes are escaped in the above example.

The Path class doesn't work well for URLs or virtual paths, but it is still useful in ASP.NET websites. For each request in your ASP.NET site, there is a Request.PhysicalPath. That value is Windows-style path that works great with the Path class.
Code that tests extensions [C#]
//
// This could be in your Global.asax file or in an ASPX page.
// It gets the physical path.
//
string physical = Request.PhysicalPath;
//
// Here we see if we are handling an ASPX file.
//
if (Path.GetExtension(physical) == ".aspx")
{
//
// Get the file name without an extension.
//
string key = Path.GetFileNameWithoutExtension(physical);
}Random file names are useful for many programs. If you need to write a temp file or log and you don't care about the path, use the parameterless Path.GetRandomFileName. You can use this for random strings, too, but that isn't its primary purpose. Here's the random string it yielded just now: zd4xcjmo.u4p.
Path.GetRandomFileName MethodThe Path type also includes two properties for separators. These are good for creating code that is easy to understand, as it is easier for some developers to read Path.DirectorySeparatorChar instead of \\. I looked at these two properties in the debugger and the results are shown below.
Path.DirectorySeparatorChar
'\\'
Path.AltDirectorySeparatorChar
'/'
There exist methods for getting temporary file names. When looking at them in the debugger, they point to a "Temp" folder in your User folder. Here are what Visual Studio's debugger says my temp file names are. Note that GetTempPath() has a separator character on the end, unlike Path.GetDirectoryName's return value.
Path.GetTempPathPath.GetTempFileName()
"C:\\Users\\allensamuel\\AppData\\Local\\Temp\\tmpC1D0.tmp"
(The output file name ends with ".tmp".)
Path.GetTempPath()
"C:\\Users\\allensamuel\\AppData\\Local\\Temp\\"
(The output path ends with the backslash character, \\.)
When accepting input from your user, your program should expect that invalid characters will be entered. For example, your program has a custom file name dialog. You want to quickly detect invalid path characters. You can use Path.GetInvalidFileNameChars and Path.GetInvalidPathChars.
In this example, we see how you can augment the character arrays of invalid characters returned by the Path.GetInvalidFileNameChars and Path.GetInvalidPathChars methods by creating a Dictionary, ensuring constant O(1) lookup time.
ToDictionary MethodProgram that gets invalid characters [C#]
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
// First, we build a Dictionary of invalid characters.
var dict = GetInvalidFileNameChars();
// Next, we test the dictionary to see if the asterisk (star) is valid.
if (dict.ContainsKey('*'))
{
// This will run, because the star is in the Dictionary.
Console.WriteLine("* is an invalid char");
}
}
/// <summary>
/// Get a Dictionary of the invalid file name characters.
/// </summary>
static Dictionary<char, bool> GetInvalidFileNameChars()
{
// This method uses lambda expressions with ToDictionary.
return Path.GetInvalidFileNameChars().ToDictionary(c => c, c => true);
}
}
Output
* is an invalid charPath is a powerful class, and other abilities it has include changing a file name's extension, determining whether the path is "rooted"—meaning whether it is relative or absolute. You can also get information about the volume, which is usually your hard drive.
Directory names. Getting the directory name of your string path is often useful, and you can see more about it on this site. Additionally, there are benchmarks of Path.GetDirectoryName.
Path.GetDirectoryName Method
File extensions. In some programs, you may need to get file extensions for many files or for string data in memory. For more details on file extensions using Path in the C# programming language, there is a useful article on the subject on this site. There is also information on how to change the extension on the path representation in memory.
Path.ChangeExtensionPath class drawbacks. I suggest that you don't use Path for URLs or virtual paths in ASP.NET. The Path type has inconsistencies with directory names: sometimes they contain an ending separator. It doesn't have lookup tables for invalid characters, so you need to use the array or a Dictionary, such as the one we saw here.
It is a common requirement to need to get lists of files in certain directories. Also, we show how you can get recursive lists of files, which are acquired by traversing subdirectories. These are not Path methods but they do return path strings.
Directory.GetFiles, Get File List Recursive File List
Some custom methods may be helpful when working with the Path class. For example, storing a list of reserved filenames and then testing to see if a filename is reserved can improve certain programs.
Path Exists Reserved Filenames
There are ways to optimize the Path methods so that they are more efficient. However, you must be careful not to change the functionality in ways that are detrimental. On this site, you can see a path method optimization that yields a three-times speedup on various inputs.
GetFileNameWithoutExtension Optimization
The Uri type provides support for website addresses and paths. It contains many helper methods you can use to specify addresses of websites.
Uri UriBuilder
We saw how you can use Path for Windows-native path manipulations and tests, in the C# language. It is ideal for file names, directory names, relative paths, file name extensions, and invalid character testing. These examples hopefully put you on the path to good file path handling in the .NET Framework.