Cette documentation est archivée et n’est pas conservée.

Environment, classe

Mise à jour : novembre 2007

Fournit des informations concernant l'environnement et la plateforme en cours ainsi que des moyens pour les manipuler. Cette classe ne peut pas être héritée.

Espace de noms :  System
Assembly :  mscorlib (dans mscorlib.dll)

[ComVisibleAttribute(true)]
public static class Environment
/** @attribute ComVisibleAttribute(true) */
public final class Environment
public final class Environment

Utilisez la classe Environment pour récupérer des informations, telles que les arguments de la ligne de commande, le code de sortie, les paramètres des variables d'environnement, le contenu de la pile des appels, le temps écoulé depuis le dernier démarrage du système ou le numéro de version du Common Language Runtime.

L'exemple suivant présente une liste d'informations relatives à l'environnement actuel.

// Sample for Environment class summary
using System;
using System.Collections;

class Sample 
{
    public static void Main() 
    {
    String str;
    String nl = Environment.NewLine;
//
    Console.WriteLine();
    Console.WriteLine("-- Environment members --");

//  Invoke this sample with an arbitrary set of command line arguments.
    Console.WriteLine("CommandLine: {0}", Environment.CommandLine);

    String[] arguments = Environment.GetCommandLineArgs();
    Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));

//  <-- Keep this information secure! -->
    Console.WriteLine("CurrentDirectory: {0}", Environment.CurrentDirectory);

    Console.WriteLine("ExitCode: {0}", Environment.ExitCode);

    Console.WriteLine("HasShutdownStarted: {0}", Environment.HasShutdownStarted);

//  <-- Keep this information secure! -->
    Console.WriteLine("MachineName: {0}", Environment.MachineName);

    Console.WriteLine("NewLine: {0}  first line{0}  second line{0}  third line",
                          Environment.NewLine);

    Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());

    Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);

//  <-- Keep this information secure! -->
    Console.WriteLine("SystemDirectory: {0}", Environment.SystemDirectory);

    Console.WriteLine("TickCount: {0}", Environment.TickCount);

//  <-- Keep this information secure! -->
    Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName);

    Console.WriteLine("UserInteractive: {0}", Environment.UserInteractive);

//  <-- Keep this information secure! -->
    Console.WriteLine("UserName: {0}", Environment.UserName);

    Console.WriteLine("Version: {0}", Environment.Version.ToString());

    Console.WriteLine("WorkingSet: {0}", Environment.WorkingSet);

//  No example for Exit(exitCode) because doing so would terminate this example.

//  <-- Keep this information secure! -->
    String query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
    str = Environment.ExpandEnvironmentVariables(query);
    Console.WriteLine("ExpandEnvironmentVariables: {0}  {1}", nl, str);

    Console.WriteLine("GetEnvironmentVariable: {0}  My temporary directory is {1}.", nl,
                           Environment.GetEnvironmentVariable("TEMP"));

    Console.WriteLine("GetEnvironmentVariables: ");
    IDictionary	environmentVariables = Environment.GetEnvironmentVariables();
    foreach (DictionaryEntry de in environmentVariables)
        {
        Console.WriteLine("  {0} = {1}", de.Key, de.Value);
        }

    Console.WriteLine("GetFolderPath: {0}", 
                 Environment.GetFolderPath(Environment.SpecialFolder.System));

    String[] drives = Environment.GetLogicalDrives();
    Console.WriteLine("GetLogicalDrives: {0}", String.Join(", ", drives));
    }
}
/*
This example produces results similar to the following:
(Any result that is lengthy or reveals information that should remain 
secure has been omitted and marked "!---OMITTED---!".)

C:\>env0 ARBITRARY TEXT

-- Environment members --
CommandLine: env0 ARBITRARY TEXT
GetCommandLineArgs: env0, ARBITRARY, TEXT
CurrentDirectory: C:\Documents and Settings\!---OMITTED---!
ExitCode: 0
HasShutdownStarted: False
MachineName: !---OMITTED---!
NewLine:
  first line
  second line
  third line
OSVersion: Microsoft Windows NT 5.1.2600.0
StackTrace: '   at System.Environment.GetStackTrace(Exception e)
   at System.Environment.GetStackTrace(Exception e)
   at System.Environment.get_StackTrace()
   at Sample.Main()'
SystemDirectory: C:\WINNT\System32
TickCount: 17995355
UserDomainName: !---OMITTED---!
UserInteractive: True
UserName: !---OMITTED---!
Version: !---OMITTED---!
WorkingSet: 5038080
ExpandEnvironmentVariables:
  My system drive is C: and my system root is C:\WINNT
GetEnvironmentVariable:
  My temporary directory is C:\DOCUME~1\!---OMITTED---!\LOCALS~1\Temp.
GetEnvironmentVariables: 
  !---OMITTED---!
GetFolderPath: C:\WINNT\System32
GetLogicalDrives: A:\, C:\, D:\

*/


// Sample for Environment class summary
import System.*;
import System.Collections.*;

class Sample
{
    public static void main(String[] args)
    {
        String str;
        String nl = Environment.get_NewLine();

        //
        Console.WriteLine();
        Console.WriteLine("-- Environment members --");

        //  Invoke this sample with an arbitrary set of command line arguments.
        Console.WriteLine("CommandLine: {0}", Environment.get_CommandLine());

        String arguments[] = Environment.GetCommandLineArgs();

        Console.WriteLine("GetCommandLineArgs: {0}",
            String.Join(", ", arguments));

        //  <-- Keep this information secure! -->
        Console.WriteLine("CurrentDirectory: {0}",
            Environment.get_CurrentDirectory());
        Console.WriteLine("ExitCode: {0}", (Int32)Environment.get_ExitCode());
        Console.WriteLine("HasShutdownStarted: {0}",
            (System.Boolean)Environment.get_HasShutdownStarted());

        //  <-- Keep this information secure! -->
        Console.WriteLine("MachineName: {0}", Environment.get_MachineName());
        Console.WriteLine("NewLine: {0}  first line{0}  second line{0}  "
            + "third line", Environment.get_NewLine());
        Console.WriteLine("OSVersion: {0}", Environment.get_OSVersion().
            ToString());
        Console.WriteLine("StackTrace: '{0}'", Environment.get_StackTrace());

        //  <-- Keep this information secure! -->
        Console.WriteLine("SystemDirectory: {0}", Environment.
            get_SystemDirectory());
        Console.WriteLine("TickCount: {0}", (Int32)Environment.
            get_TickCount());

        //  <-- Keep this information secure! -->
        Console.WriteLine("UserDomainName: {0}", 
            Environment.get_UserDomainName());
        Console.WriteLine("UserInteractive: {0}",
            (System.Boolean)Environment.get_UserInteractive());

        //  <-- Keep this information secure! -->
        Console.WriteLine("UserName: {0}", Environment.get_UserName());
        Console.WriteLine("Version: {0}", Environment.get_Version().
            ToString());
        Console.WriteLine("WorkingSet: {0}",
            System.Convert.ToString(Environment.get_WorkingSet()));

        //  No example for Exit(exitCode) because doing so would terminate 
        // this example.
        //  <-- Keep this information secure! -->
        String query = "My system drive is %SystemDrive% and my system "
            + "root is %SystemRoot%";

        str = Environment.ExpandEnvironmentVariables(query);
        Console.WriteLine("ExpandEnvironmentVariables: {0}  {1}", nl, str);
        Console.WriteLine("GetEnvironmentVariable: {0}  My temporary "
            + "directory is {1}.", nl, Environment.
            GetEnvironmentVariable("TEMP"));
        Console.WriteLine("GetEnvironmentVariables: ");

        IDictionary environmentVariables = Environment.
            GetEnvironmentVariables();
        DictionaryEntry de;
        IEnumerator enumObj = environmentVariables.GetEnumerator();
        while (enumObj.MoveNext()) {
            de = (DictionaryEntry)enumObj.get_Current();
            Console.WriteLine("  {0} = {1}", de.get_Key(), de.get_Value());
        }
        Console.WriteLine("GetFolderPath: {0}", 
            Environment.GetFolderPath(Environment.SpecialFolder.System));
        String drives[] = Environment.GetLogicalDrives();
        Console.WriteLine("GetLogicalDrives: {0}", 
            String.Join(", ", drives));
    } //main
} //Sample
/*
This example produces results similar to the following:
(Any result that is lengthy or reveals information that should remain 
secure has been omitted and marked "!---OMITTED---!".)

C:\>env0 ARBITRARY TEXT

-- Environment members --
CommandLine: env0 ARBITRARY TEXT
GetCommandLineArgs: env0, ARBITRARY, TEXT
CurrentDirectory: C:\Documents and Settings\!---OMITTED---!
ExitCode: 0
HasShutdownStarted: False
MachineName: !---OMITTED---!
NewLine:
  first line
  second line
  third line
OSVersion: Microsoft Windows NT 5.1.2600.0
StackTrace: '   at System.Environment.GetStackTrace(Exception e)
   at System.Environment.GetStackTrace(Exception e)
   at System.Environment.get_StackTrace()
   at Sample.Main()'
SystemDirectory: C:\WINNT\System32
TickCount: 17995355
UserDomainName: !---OMITTED---!
UserInteractive: True
UserName: !---OMITTED---!
Version: !---OMITTED---!
WorkingSet: 5038080
ExpandEnvironmentVariables:
  My system drive is C: and my system root is C:\WINNT
GetEnvironmentVariable:
  My temporary directory is C:\DOCUME~1\!---OMITTED---!\LOCALS~1\Temp.
GetEnvironmentVariables: 
  !---OMITTED---!
GetFolderPath: C:\WINNT\System32
GetLogicalDrives: A:\, C:\, D:\

*/


Tous les membres static (Shared en Visual Basic) publics de ce type sont thread-safe. Il n'est pas garanti que les membres d'instance soient thread-safe.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professionnel Édition x64, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile pour Smartphone, Windows Mobile pour Pocket PC, Xbox 360

Le .NET Framework et le .NET Compact Framework ne prennent pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.

.NET Framework

Pris en charge dans : 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Pris en charge dans : 3.5, 2.0, 1.0

XNA Framework

Pris en charge dans : 2.0, 1.0
Afficher: