The System.IO.Path class is a utility class that has methods for constructing paths of files and directories. Although you can simply use simple string concatenation and methods, using the Path class makes it easier for you when dealing with paths. The following table shows some methods of the Path class.

Methods Description
ChangeExtention Changes the extention of a particular path.
Combine Combines an array of paths.
GetDirectoryName Returns the directory information for the specified path string.
GetExtension Returns the extension of the specified path string.
GetFileName Returns the file name and extension of the specified path string.
GetFullPath Returns the absolute path for the specified path string.
HasExtension Determines whether a path includes a file name extension.

Figure 1 – System.IO.Path Methods


The ChangeExtension method is used to change the extension of a specified path. Supposed we have given a path C:path.txt, the extension name here is .txt. The code below demonstrates how to change the extension of a path.

string path = Path.ChangeExtension(@"C:path.txt", "bmp");

The ChangeExtension method accepts two string arguments and returns the modified path as a string. The first parameter is the path containing the extension that we will modify. The second parameter is the replacement extension.


The Combine method is used to combine multiple paths. For example, suppose you have a path C:Documents and you have another path named sample.txt, then using the Combine method will combine these two paths to produce C:Documentssample.txt. Note that a backslash was automatically inserted between the two paths.

string path1 = @"C:Document";
string path2 = @"sample.txt";

string newPath = Path.Combine(path1, path2);


You might just use some string concatenation but using the Combine method is easier and automated.


The method GetDirectoryName gets the path of the parent directory of a given directory or file path. Consider the following example.

string path1 = @"C:Parentsample.txt";
string path2 = @"C:ParentChild";

string parent1 = Path.GetDirectoryName(path1);
string parent2 = Path.GetDirectoryName(path2);



The GetExtension method returns the extension part of the path. The extension identifies the type of file and the program that will open it.

string path1 = @"C:Parentsample.txt";


This method returns the full name including the extension of the file in a path.

string path1 = @"C:Parentsample.txt";


This method returns the absolute path of a given relative path. Consider the following example code.

string path = "sample.txt";
C:UsersVSDocumentsVisual Studio 2010ProjectsSimpleProgramSimpleProgram

The absolute path returned is based on the absolute path of the current program. Changing the location of the program also changes the output of this method.


This method simply returns true if the given path has a file extension, and false, if it has does not contain an extension. Therefore, you can also use this method to determine if the path is a file path or a directory path since directories contain no extension.