| by Arround The Web | No comments

Environment.SystemDirectory Property

If you want to get the path where your C# file is specified, the Environment class in C# supports the SystemDirectory property.

Environment.SystemDirectory

This property is available in the Environment Class. It provides an information about the working environment like variables, methods used, and system related information in C#.

The SystemDirectory property is used to return the full path where the file is stored. It is possible to store the C# files in any number of folders starting from the root folder like inside C drive, D drive, etc.

Syntax:

string Environment.SystemDirectory

Return:

Return the entire path.

Example 1:

Let’s store the file in the previously specified path and run it.

using System;
class Linuxhint {
    //let's implement SystemDirectory property inside main method
  static public  void Main() {
      //get the path in which the program file is resided.
    Console.WriteLine("Path: "+Environment.SystemDirectory);
  }
}

Output:

Path:D:\Windows\system32\linuxhint\July\C#

Explanation:

We implement the property inside the main() method.

Line 6: Console.WriteLine(“Path: “+Environment.SystemDirectory);

We directly return the path without storing it in the variable.

Example 2:

We can also store the path into a string type variable and return it.

using System;
class Linuxhint {
    //let's implement SystemDirectory property inside main method
  static public  void Main() {
      //get the path in which the program file is resided.
     string way=Environment.SystemDirectory;
    Console.WriteLine("Path: "+way);
  }
}

Output:

Path:D:\Windows\system32\linuxhint

We create a string variable “way”, store and display the path.

Explanation:

We implement the property inside the main() method.

Line 6: string way=Environment.SystemDirectory;

We store the path inside the variable “way”.

Line 7: Console.WriteLine(“Path: “+way);

Display the path present in the variable using the Console.WriteLine().

Conclusion

With this article, we came to know that it is possible to return the path where our C# file is located in our system using the Environment.SystemDirectory property. It returns the path in the form of a string. You can implement this in your machine and get to know your file path.

Share Button

Source: linuxhint.com

Leave a Reply