We can compare two strings for equality using several ways. For example, we can use…
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.
|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"); Console.WriteLine(path);
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:\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);
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:\Parent\sample.txt"; string path2 = @"C:\Parent\Child"; string parent1 = Path.GetDirectoryName(path1); string parent2 = Path.GetDirectoryName(path2); Console.WriteLine(parent1); Console.WriteLine(parent2);
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));
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));
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.
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.