Use of System.Environment Class

The System.Environment class is a fundamental class in .NET that offers valuable insights into the current environment and platform. It serves as a gateway for retrieving various information such as environment variable settings, the version of the common language runtime (CLR) in use, and the contents of the call stack. This class plays a crucial role in providing essential details and system-level functionality to developers.

System.Environment Class

One notable characteristic of the System.Environment class is its inability to be inherited. This design choice ensures that the class maintains its core functionality and prevents the creation of subclasses that could potentially introduce inconsistencies or alter its intended behavior. By prohibiting inheritance, the class preserves its integrity and guarantees the consistency of its methods and properties across different applications.

Developers can use the capabilities of the System.Environment class to access a wide range of system-related information, enabling them to make informed decisions and tailor their code accordingly. Whether it's retrieving environment variables, obtaining runtime version details, or inspecting the call stack, this class serves as a reliable source of crucial system-related data.

How to get Current working directory ?

System.Environment.CurrentDirectory // will return your current working directory

How to get Machine Name ?

System.Environment.MachineName // will return your current machine name.

How to get current Operating System Version ?

System.Environment.OSVersion.ToString () // will return your current operating system version

How to get the current user name of the system ?

System.Environment.UserName // will return your current user name

How to add a New line in a text file ?

C# Source Code
using System; using System.IO; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { System.IO.TextWriter writeFile = new StreamWriter("c:\\newline.txt"); writeFile.WriteLine("Before New Line"); writeFile.WriteLine(System.Environment.NewLine); writeFile.WriteLine("After New Line"); writeFile.Flush(); writeFile.Close(); writeFile = null; } catch (IOException ex) { MessageBox.Show(ex.ToString()); } } } }

How to add a blank line in a text file ?

VB.Net Source Code
Imports System.IO Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim writeFile As System.IO.TextWriter = New StreamWriter("c:\newline.txt") writeFile.WriteLine("Before New Line") writeFile.WriteLine(System.Environment.NewLine) writeFile.WriteLine("After New Line") writeFile.Flush() writeFile.Close() writeFile = Nothing Catch ex As IOException MessageBox.Show(ex.ToString()) End Try End Sub End Class


The System.Environment class is a non-inheritable class that provides essential information about the current environment and platform. Its availability allows developers to access valuable system-level details, empowering them to build robust and platform-aware applications within the .NET framework.