.NET
  Home arrow .NET arrow Page 4 - Knowing Your Environment: the System.Envir...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Knowing Your Environment: the System.Environment Class
By: Ayad Boudiab
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-05-12

    Table of Contents:
  • Knowing Your Environment: the System.Environment Class
  • More Properties
  • More Explanations
  • Methods

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:


     

    Test.exe

    a

    b

    c

    [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.

       · Hello everyone,Here is my latest article on the System.Environment class.Please...
     

    .NET ARTICLES

    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT