Open In App

C# Path Class – Basics Operations

Last Updated : 26 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:\GeeksforGeeks\file.txt";
    string path2 = @"c:\GeeksforGeeks\file";
    string path3 = @"GeeksforGeeks";
      
      // 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:\GeeksforGeeks\file.txt has an extension.
c:\GeeksforGeeks\file has no extension.
The string GeeksforGeeks has no root information.
The full path of GeeksforGeeks is /home/dqcp7B/GeeksforGeeks.
/tmp/58wsOH/ is the path of temporary files.
/tmp/58wsOH/tmp5990a59.tmp is available for use.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads