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

ChangeExtension()


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");
Console.WriteLine(path);
C:\path.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.

Combine()


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:\Documents\sample.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);

Console.WriteLine(newPath);
C:\Document\sample.txt

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

GetDirectoryName()


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

string path1 = @"C:\Parent\sample.txt";
string path2 = @"C:\Parent\Child";

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

Console.WriteLine(parent1);
Console.WriteLine(parent2);
C:\Parent
C:\Parent

GetExtension()


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:\Parent\sample.txt";
Console.WriteLine(Path.GetExtension(path));
.txt

GetFileName()


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

string path1 = @"C:\Parent\sample.txt";
Console.WriteLine(Path.GetFileName(path));
sample.txt

GetFullPath()


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

string path = "sample.txt";
Console.WriteLine(Path.GetFullPath(path));
C:\Users\VS\Documents\Visual Studio 2010\Projects\SimpleProgram\SimpleProgram\
bin\Release\sample.txt

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.

HasExtension()


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.

Written by compitionpoint

Leave a Comment

Your email address will not be published. Required fields are marked *