Lire en anglais

Partager via


Directory.GetCurrentDirectory Méthode

Définition

Obtient le répertoire de travail en cours de l'application.

C#
public static string GetCurrentDirectory ();

Retours

Chaîne qui contient le chemin d’accès absolu du répertoire de travail actuel et qui ne se termine pas par une barre oblique inverse (\).

Exceptions

L'appelant n'a pas l'autorisation requise.

Le système d’exploitation est Windows CE, qui ne dispose pas de fonctionnalité par rapport au répertoire actif.

Cette méthode est disponible dans le .NET Compact Framework, mais n’est pas prise en charge actuellement.

Exemples

L'exemple suivant décrit comment utiliser la méthode GetCurrentDirectory.

C#
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            // Get the current directory.
            string path = Directory.GetCurrentDirectory();
            string target = @"c:\temp";
            Console.WriteLine("The current directory is {0}", path);
            if (!Directory.Exists(target))
            {
                Directory.CreateDirectory(target);
            }

            // Change the current directory.
            Environment.CurrentDirectory = (target);
            if (path.Equals(Directory.GetCurrentDirectory()))
            {
                Console.WriteLine("You are in the temp directory.");
            }
            else
            {
                Console.WriteLine("You are not in the temp directory.");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

Remarques

Le répertoire actif est distinct du répertoire d’origine, qui est celui à partir duquel le processus a démarré.

Pour obtenir la liste des tâches d’E/S courantes, consultez Tâches courantes d’E/S.

S’applique à

Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Voir aussi