{%Pragmatic Coding%} Tips and tricks for software developers.


How To: Using System.IO.Path in C#

There is different effective ways of dealing with filenames and paths that the .NET Framework provides. 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 on servers. The Path type makes handling these easier.

This article uses the C# Path type. Path handles file locations in a consistent way. It resolves common problems.

Example 1

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#]


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.

Example 2

It is useful to see the results of the Path methods on various inputs. In some cases, 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.

(Text was reformatted for clarity.)

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 Extension


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".

Path methods in Visual Studio debugger

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#]


Syntax 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.

Escape slashes

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.

ASP.NET paths

The Path class doesn't work well for URLs or virtual paths, but it is still very 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#]

Random file names

Random file names are very 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 Method

Separator characters

The 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.



Temporary file names

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.


(The output file name ends with ".tmp".)

(The output path ends with the backslash character, \\.)

Invalid characters

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 Method


Path 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 very 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 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.

File lists

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 ListRecursive 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 ExistsReserved 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 ClassUriBuilder Class


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.

Source: C# Path Examples

Share on social network

Share to Facebook
Share to Google Plus
Share to LiveJournal
Share to MyWorld
Share to Odnoklassniki
Share to Yandex
Filed under: .NET, C# Leave a comment
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.