This type returns information about the operating system context. It offers commonly-needed properties such as NewLine
.
Environment methods and properties return information about the operating system and execution environment. We test these methods.
This method does not run finally statements. It provides a way to kill a process in its physical sense on the operating system.
Exit()
method is located in the System
namespace. It is a static
method that receives an int
—this is used as the exit code.using System; class Program { static void Main() { try { // // Calls the Environment.Exit method and returns a zero status code. // ... The finally statement is never reached. // Environment.Exit(0); } finally { Console.WriteLine("Finally statement"); } } }
ExpandEnvironmentVariables
In the Windows operating system environment variables are used. They insert strings specific to the operating system's current setup.
ExpandEnvironmentVariables
method, we expand these encoded variables.string
array of environment variable string
literals (they begin and also end with percent characters).ExpandEnvironmentVariables
on each string
literal and write the substituted result.using System; class Program { static void Main() { string[] variables = { "%WINDIR%", "%HOMEDRIVE%", "%USERNAME%", "%COMPUTERNAME%" }; foreach (string v in variables) { string result = Environment.ExpandEnvironmentVariables(v); Console.WriteLine(result); } } }C:\Windows C: Sam2 SAM-PC2
GetCommandLineArgs
You can receive command line arguments only in the Main
entry point in the C# language. But the GetCommandLineArgs
method can be used anywhere.
Main
. It also shows how to use GetCommandLineArgs
.GetCommandLineArgs
is the program executable path.using System; class Program { static void Main(string[] args) { // Args does not contain the program path. Console.WriteLine(string.Join(",", args)); // GetCommandLineArgs contains the program path as the first element. string[] array = Environment.GetCommandLineArgs(); Console.WriteLine(string.Join(",", array)); } }(Argument: dot net perls) (Second output line truncated.) dot,net,perls C:\Users\...\bin\Release\test.exe,dot,net,perls
GetFolderPath
This returns a path string
. It receives an enum
of type Environment.SpecialFolder
. It returns a string
that depends on the user's current operating environment.
using System; class Program { static void Main() { string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); Console.WriteLine(path); } }Varies depending on the operating environment. C:\Users\Sam2\Desktop
GetLogicalDrives
With this method, you can determine where to search for files on local drives. It returns a string
array containing drive volumes.
using System; class Program { static void Main() { string[] value = Environment.GetLogicalDrives(); Console.WriteLine(string.Join(",", value)); } }C:\,D:\,H:\
ProcessorCount
Processors can have multiple cores. The ProcessorCount
property in the .NET Framework returns the total number of cores on a computer, not processors.
Environment.ProcessorCount
returns an int
. It indicates how many cores all your computer's processors have.ProcessorCount
.using System; class Program { static void Main() { Console.WriteLine(Environment.ProcessorCount); } }2
SpecialFolder
Microsoft Windows has special folders. For example, Microsoft Word will save to Documents. We use the Environment.SpecialFolder
enum
to locate these special folders.
Environment.SpecialFolder
, press period and get a list of values.MyDocuments
. This is an ideal place for business files. We combine SpecialFolder
with GetFolderPath
.Desktop DesktopDirectory MyDocuments MyMusic MyPictures Programs StartMenu Startuppublic partial class ExampleForm : Form { // Initialize the controls on the form in the constructor here. public ExampleForm() { InitializeComponent(); openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); } }
GetFolderPath
exampleSpecialFolder
is not restricted to usage with InitialDirectory
. GetFolderPath
converts the enum
into a usable string
by the system.
private void TestMethod() { // // Store the location of Pictures folder in a string. // Environment.SpecialFolder special = Environment.SpecialFolder.MyPictures; string folderLocation = Environment.GetFolderPath(special); }
NewLine
On the Windows operating system, the newline is "\r\n". This is two characters. The Environment.NewLine
property returns this string
literal.
The Environment type is a way to interact with the operating system environment. It eliminates the need to use interoperability calls to get this information.