C# Path Class – Basics Operations
C# path class comes under System.IO namespace and System.Runtime.dll assembly. This class is used to perform operations on string instances that have file path or directory path information. A path is a string that holds the location of the file or directory and it can be an absolute or relative location. Such operations are performed in a cross-platform manner. This class allows us to apply a variety of operations that are related to a temporary file location.
Syntax:
public static class Path
Operations of path class: There are numerous operations that can be performed on a string object that contains directly path information. Some of these have been discussed below in detail:
1. HasExtension(): We can check whether a string object that contains file path or directory information has an extension or not using the HasExtension() method provided by the Path class.
Syntax:
HasExtension(path);
Here, path is a string object that contains a file path or directly information
Return Type:
- true: If the path (string object) is a rooted path
- false: If the path (string object) is not a rooted path
2. IsPathRooted(). We can check whether a string object that contains file path or directory information is rooted using the IsPathRooted() method of the Path class.
Syntax:
IsPathRooted(path)
Here, path is a string object that contains a file path or directly information
Return Type:
- true: If the path (string object) is a rooted path
- false: If the path (string object) is not a rooted path
3. GetFullPath(): To get the full path of a temporary we can use the GetFullPath() method.
Syntax:
GetFullPath(path)
Here, path is a string object that contains a file path or directly information
Return Type: Returns the full path of a temporary file.
4. GetTempPath(): To get the location of temporary files, the GetTempPath() method can be used.
Syntax:
GetTempPath()
Return Type: Returns the location where temporary files are saved.
5. GetTempFileName(): To get the temporary file name that is available for use, GetTempFileName() method can be used.
Syntax:
GetTempFileName()
Return Type: Returns a temporary file name that is available for use
Example:
In this program, we have used three string objects, path1, path2, and path3 that contain three different file paths and we have applied the operations discussed before on these string objects.
C#
// C# program to demonstrate the operations of path class using System; using System.IO; class GFG{ static public void Main() { // Initialize strings containing path string path1 = @"c:\w3wiki\file.txt" ; string path2 = @"c:\w3wiki\file" ; string path3 = @"w3wiki" ; // Check whether path1 has extension if (Path.HasExtension(path1)) { Console.WriteLine( "{0} has an extension." , path1); } // Check whether path2 has extension if (!Path.HasExtension(path2)) { Console.WriteLine( "{0} has no extension." , path2); } // Check whether path3 is rooted if (!Path.IsPathRooted(path3)) { Console.WriteLine( "The string {0} has no root information." , path3); } Console.WriteLine( "The full path of {0} is {1}." , path3, Path.GetFullPath(path3)); Console.WriteLine( "{0} is the path of temporary files." , Path.GetTempPath()); Console.WriteLine( "{0} is available for use." , Path.GetTempFileName()); } } |
Output:
c:\w3wiki\file.txt has an extension. c:\w3wiki\file has no extension. The string w3wiki has no root information. The full path of w3wiki is /home/dqcp7B/w3wiki. /tmp/58wsOH/ is the path of temporary files. /tmp/58wsOH/tmp5990a59.tmp is available for use.
Contact Us