Get Along With System.Environment Class

Introduction

In this article we will have a detailed article on System.Environment. Why is it so serious to have it? I don't know about you, but my personal experience says that, knowing your users is as important as knowing your user's environment.

Actually, this class will provide you accessibility to your user's environment.

Background

If you are familiar with your computer then, it is enough for you to deal with it.

Why do I Need It?

Suppose you have a solution file in which you want to save an x-file on user's environment. Then, you must know the partitioned structure of the user's computer. If you want to save a file in My Documents, then you must know the actual path of the My Documents folder.

To do this, you want something that lets you know about this.

For this, we have a class, Environment, in the System namespace of the .NET Library.

Architecture of Environment Class

All the methods and properties are static in nature. The following are the methods:

  • CurrentDirectory

    Gets/sets the path of the current working directory (folder).
     

    // Current Working Directory

    Console.WriteLine("My Working Directory is : \t" + Environment.CurrentDirectory);

    Console.WriteLine("-----------------------------------------------------------------");
     
  • CommandLine

    Gets/sets the path of the project's command line inside the Bin folder of the solution.
     

    //CommandLine Property

    Console.WriteLine("My Command Line is : \t" + Environment.CommandLine);

    Console.WriteLine("-----------------------------------------------------------------");
     

  • MachineName

    Gets/sets the name of the Working Machine.
     

    //Machine Name of Computer

    Console.WriteLine("My Machine Name is  : \t" + Environment.MachineName);

    Console.WriteLine("-----------------------------------------------------------------");
     

  • Processor Count

    Gets/sets the number of processors inside your CPU.
     

    //Proccessor Count

    Console.WriteLine("Proccessor Count of My Computer is : \t"+Environment.ProcessorCount);

    Console.WriteLine("--------------------------------------------------------------------");
     

  • Is64bitOrNot

    Returns a Boolean value if the current environment is 64-bit compatible or not.
     

    // Is 64bit Or Not
    Console.WriteLine("Is My Computer is 64Bit Comaptible : \t" + Environment.Is64BitOperatingSystem);

    Console.WriteLine("-----------------------------------------------------------------");
     

  • OsVersion

    Returns the name of the Operating System.
     

    //OS Version

    Console.WriteLine("Version of The OS is : \t"+Environment.OSVersion);

    Console.WriteLine("-----------------------------------------------------------------");
     

  • UserName

    Returns the active Username of the user.
     

     //UserName

    Console.WriteLine("My Current User Name is : \t" + Environment.UserName);

    Console.WriteLine("-----------------------------------------------------------------");
     

  • SystemDirectory

    Returns the path of the System32 folder.
     

    // System.Directory

    Console.WriteLine("My  system Directory is : \t" + Environment.SystemDirectory);

    Console.WriteLine("-----------------------------------------------------------------");
     

  • PageSize

    Provides the page size assigned by your computer processor.
     

    //System Page Size

    Console.WriteLine("Page Size of The System is : \t"+Environment.SystemPageSize);

    Console.WriteLine("-----------------------------------------------------------------");
     

  • Version

    Returns the .NET framework version.
     

    // Version

    Console.WriteLine("Version of .NET : \t"+Environment.Version);

    Console.WriteLine("-----------------------------------------------------------------");

The following are some more static methods:

  • GetLogicalDrives()

    Returns an array of strings that has all the Logical drive's names.
     

    //Get Logical drives

    string[] myDrives = Environment.GetLogicalDrives();

    foreach (string drive in myDrives)

    {

         Console.WriteLine(drive);
    }
     

  • GetFolderPath()

    Returns the path of the folder ed as an argument in its constructor.
     

    //Get Folder Path

    string myDocument = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    Console.WriteLine(myDocument);

Here, we have tried to get the path of My Documents.

Output

I have an enclosed Solution file that has an output like:

 Environment Class 


Similar Articles