Open In App

C# Program to Get the Full Path of the Current Directory Using Environment Class

Last Updated : 01 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In C#, Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that it retrieves command-line arguments information, exit codes information, environment variable settings information, contents of the call stack information, and time since the last system boot in milliseconds information. In this article, we will discuss how to get the full path of the current directory. So to solve this problem we use the CurrentDirectory property of the Environment Class. This property returns the complete path of the current working directory of your computer. This property also throws the following exceptions:

  • ArgumentException: This exception is thrown when the CurrentDirectory property tries to set to an empty string.
  • ArgumentNullException: This exception is thrown when the CurrentDirectory property tries to set to null.
  • IOException: This exception is thrown when the input/output error occurred.
  • DirectoryNoFoundException: This exception is thrown when the CurrentDirectory property tries to set a local path/address that cannot be found.
  • SecurityException: This exception is thrown when the caller does not have suitable permission.

Syntax:

Environment.CurrentDirectory

Return Type: The return type of this property is a string. And the returned string represents the current directory path.

Example:

C#




// C# program to find the current directory path
// Using Environment Class
using System;
  
class GFG{
      
static public void Main()
{
      
    // Declaring a string
    string resultPath = "";
      
    // Get the complete path of the current
    // working directory. Using 
    // CurrentDirectory property of 
    // Environment class
    resultPath = Environment.CurrentDirectory;
      
    Console.WriteLine("System Directory:\n" + resultPath);
}
}


Output:

System Directory:
/Users/Projects/newprogram/

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

Similar Reads