Environment Type
This page was last reviewed on Dec 10, 2021.
Dot Net Perls
Environment. This 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. We test these methods.
Exit. This method 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.
Detail This method is located in the System namespace. It is a static method that receives an int—this is used as the exit code.
Detail The finally block is not executed if the physical process is terminated directly (by Exit).
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.
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 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 args
Next This program shows how the args parameter is received in Main. It also shows how to use GetCommandLineArgs.
Note The first element in the string array returned by 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.
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.
using System; class Program { static void Main() { string[] value = Environment.GetLogicalDrives(); Console.WriteLine(string.Join(",", value)); } }
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.
using System; class Program { static void Main() { Console.WriteLine(Environment.ProcessorCount); } }
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.
Desktop DesktopDirectory MyDocuments MyMusic MyPictures Programs StartMenu Startup
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.
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.
String Literal
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 10, 2021 (edit).
© 2007-2024 Sam Allen.