HomeSearch

C# Environment Type: Exit, SpecialFolder

This C# page describes the Environment type, which returns information about the OS.
Environment. The Environment type returns information about the operating system context. It offers commonly-needed properties such as NewLine.
Methods and properties. Environment methods and properties return information about the operating system and execution environment.
Exit. Environment.Exit does not run finally statements. It provides a way to kill a process in its physical sense on the operating system.

Note: Clean-up of your program's resources does not occur. Finally statements are ignored.

Finally

System: This method is located in the System namespace. It is a static method that receives an int—this is used as the exit code.

And: It asserts physical control on the process, disobeying the remaining logic in the high-level C# code.

C# program that calls Environment.Exit with finally 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"); } } }
Notes, above example. We see the try-finally control structure. This block indicates a protected region of the code, where no matter what happens, the finally block is executed afterwards.

However: The finally block is not executed if the physical process is terminated directly.

So: Environment.Exit terminates the process and the statement in the finally block is never executed, as we see by running the program.

Threading: Because Environment.Exit acts upon a process, all threads will be terminated with no warning if you call it.

ExpandEnvironmentVariables. In the Windows operating system environment variables are used. They insert strings specific to the operating system's current setup.

Info: With the Environment ExpandEnvironmentVariables method, we expand these encoded variables.

Note: This program loops over a string array of environment variable string literals (they begin and also end with percent characters).

Next: We call Environment.ExpandEnvironmentVariables on each string literal and write the substituted result.

C# program that expands environment variables 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); } } } Output C:\Windows C: Sam2 SAM-PC2
Notes, variables. ExpandEnvironmentVariables will expand more than one variable in a string in a call. RANDOM and DATE are not supported according to my testing.
NewLine. On the Windows operating system, the newline is "\r\n". This is two characters. The Environment.NewLine property returns this string literal.Environment.NewLineString Literal
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 args

Next: This program shows how the args parameter is received in Main. It also shows how to use Environment.GetCommandLineArgs.

Note: The first element in the string array returned by GetCommandLineArgs is the program executable path.

Array
C# program that uses Environment.GetCommandLineArgs 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)); } } Output (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.
C# program that calls GetFolderPath using System; class Program { static void Main() { string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); Console.WriteLine(path); } } Output Varies depending on the operating environment. C:\Users\Sam2\Desktop
Performance, paths. I tested the performance of GetFolderPath. The method required 7179 nanoseconds per call, which is 7 microseconds.

Note: If you stored the result of GetFolderPath in a static string, you could load that in a couple nanoseconds.

Thus: It would be much faster—3000 times faster—to cache the result of GetFolderPath.

GetLogicalDrives. With the Environment.GetLogicalDrives method, you can determine where to search for files on local drives. It returns a string array.

Tip: Logical drives may not be separate physically, but to the Windows operating system, they are "logically" separate.

Tip 2: A partition on a hard disk is a logical drive. A USB disk is also a logical as well as physical drive.

C# program that shows GetLogicalDrives using System; class Program { static void Main() { string[] value = Environment.GetLogicalDrives(); Console.WriteLine(string.Join(",", value)); } } Output 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.

Tip: Environment.ProcessorCount returns an int. It indicates how many cores all your computer's processors have.

Int, uint

Note: This program shows that the dual-core, single processor computer I execute it on returns 2 for ProcessorCount.

C# program that accesses ProcessorCount using System; class Program { static void Main() { Console.WriteLine(Environment.ProcessorCount); } } Output 2
Notes, ProcessorCount. This returns the number of logical processors in the executing computer. Each core is considered a discrete processor, rather than part of a single processor.

So: You can use this property for creating an array of Threads to fully use the computer's computational resources.

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.

Tip: In Visual Studio, we can type in Environment.SpecialFolder, press period and get a list of values.

Next: For our example, we will use MyDocuments. This is an ideal place for business files. We combine SpecialFolder with GetFolderPath.

Locations: Desktop DesktopDirectory MyDocuments MyMusic MyPictures Programs StartMenu Startup C# program that uses Environment.SpecialFolder public 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 example. SpecialFolder is not restricted to usage with InitialDirectory. GetFolderPath converts the enum into a usable string by the system.
C# program that uses Environment.GetFolderPath private void TestMethod() { // // Store the location of Pictures folder in a string. // Environment.SpecialFolder special = Environment.SpecialFolder.MyPictures; string folderLocation = Environment.GetFolderPath(special); }
A summary. 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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls