Knowing Your Environment: the System.Environment Class - More Properties
(Page 2 of 4 )
CurrentDirectory : This property gets or sets the fully qualified path of the current working directory. Keep in mind that the current directory will contain a trailing slash if the process starts in the root directory. Otherwise, there will be no trailing slash. To print the current directory, use the following statement:
//returns a string containing a directory path
Console .WriteLine( "The directory path is: " + Environment .CurrentDirectory);
ExitCode : The result of this property is a 32-bit signed integer that contains the exit code of the process. Why would I need the exit code of the process? Glad you asked. The Main() method is the entry and exit points of an application. There are multiple versions of the Main() method. Our concern, however, is with the return type. Main() can return a void or an int. The int value reports to the calling program (probably a script) the status information. A 0 means that the program ran successfully. Other values means there are errors. However, how would you know the status when Main() returns void? The answer is: ExitCode. Just use the ExitCode property to get or set the exit code of the process:
//returns 0 by default, which means success!
Console .WriteLine( "The ExitCode is: " + Environment .ExitCode);
HasShutdownStarted: This property returns a Boolean value (true/false) to indicate whether the common language runtime is shutting down or the current application domain is unloading (in .NET, assemblies are not hosted directly in a process. They are hosted in an application domain). Use this property in an object's finalization method. If it returns true, then you cannot reliably access any object that has a finalization method and is referenced by a static field.
//returns a boolean value that indicates whether the common language runtime
//is shutting down or the application domain is unloading
Console .WriteLine( "Has shutdown started? {0}" ,
E nvironment .HasShutdownStarted);
MachineName: This property returns a string containing the name of the computer.
//returns a string containing the name of this computer
Console .WriteLine( "MachineName: {0}" , Environment .MachineName);
NewLine: Using this property results in a new line inserted in the string. You can use n, but in some platforms you might need rn. To be on the safe side, use Environment.NewLine consistently.
//A string containing "rn" for non-Unix platforms,
//or a string containing "n" for Unix platforms.
Console .WriteLine( "Line 1{0}Line 2" , Environment .NewLine);
OSVersion: This property gets an OperatingSystem object that contains the current platform identifier and version number. Knowing the operating system version on which your application is running is extremely important; it helps you take advantage of some of the functionalities that exist in one version of the OS and not in another.
//Gets an OperatingSystem object that contains the current platform //identifier and version number.
Console .WriteLine( "OS Version: {0}" , Environment .OSVersion.ToString());
ProcessorCount: Knowing the number of processors on the machine will help you take advantage of multiprocessing. This property will help you discover that value.
//Gets the number of processors on the current machine.
Console .WriteLine( "The number of processors on this computer is {0}." ,
Environment .ProcessorCount);
Next: More Explanations >>
More .NET Articles
More By Ayad Boudiab