Knowing Your Environment: the System.Environment Class - Methods
(Page 4 of 4 )
GetCommandLineArguments(): The first element in the string array returned is the executable file name. The following elements contain the remaining command-line arguments. For example:
Test.exe a b c
Means that the array of the command line arguments has the following data:
[0] [1] [2] [3]
//Returns a string array containing the command line arguments for the
//current process.
String [] arguments = Environment .GetCommandLineArgs();
Console .WriteLine( "GetCommandLineArgs: {0}" , String .Join( ", " , arguments));
GetEnvironmentVariable() and GetEnvironmentVariables(): With these methods, you can retrieve one environment variable (GetEnvironmentVariable()) or a dictionary (key-value pairs) of all the environment variables (GetEnvironmentVariables()). You can also use the SetEnvironmentVariable() method to create, modify, or delete an environment variable.
// Change the directory to %WINDIR%
Environment .CurrentDirectory = Environment .GetEnvironmentVariable( "windir" );
DirectoryInfo info = new DirectoryInfo ( "." );
Console .WriteLine( "Directory Info: " + info.FullName);
//Retrieves all environment variable names and their values from the
//current process. The return type is a dictionary that you can step through //using a foreach loop. Every element is actually a pair of key and value.
IDictionary environmentVariables = Environment .GetEnvironmentVariables();
Console .WriteLine( "Environment variables:n" );
foreach ( DictionaryEntry de in environmentVariables)
{
Console .WriteLine( "{0} = {1}" , de.Key, de.Value);
}
GetLogicalDrives(): By retrieving the logical drives, you can (for instance) determine which one represents the flash drive inserted by the user, and take appropriate actions in your application.
//Returns an array of string containing the names of the logical drives
//on the current computer.
String [] drives = Environment .GetLogicalDrives();
Console .WriteLine( "Logical Drives: {0}" , String .Join( ", " , drives));
I hope this article served its purpose of making the developer aware of the existence of this class, and what functionality it provides. Many of the methods and properties listed above help the developer customize the application depending upon the environment under which it is running. After all, if the application is running on Windows Vista, it is favorable to adjust the application features and benefit from the cool functionality that Vista provides. The user will appreciate such minor adjustments, and your application will get a thumbs up in the usability engineering arena.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |