Directory.GetCurrentDirectory Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient le répertoire de travail actuel de l’application.
public:
static System::String ^ GetCurrentDirectory();
public static string GetCurrentDirectory();
static member GetCurrentDirectory : unit -> string
Public Shared Function GetCurrentDirectory () As String
Retours
Chaîne qui contient le chemin absolu du répertoire de travail actuel et 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 n’a pas de fonctionnalité d’annuaire actuel.
Cette méthode est disponible dans le .NET Compact Framework, mais n’est pas prise en charge actuellement.
Exemples
L’exemple suivant montre comment utiliser la GetCurrentDirectory méthode.
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 (target.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());
}
}
}
open System
open System.IO
try
// Get the current directory.
let path = Directory.GetCurrentDirectory()
let target = @"c:\temp"
printfn $"The current directory is {path}"
if not (Directory.Exists target) then
Directory.CreateDirectory target |> ignore
// Change the current directory.
Environment.CurrentDirectory <- target
if path.Equals(Directory.GetCurrentDirectory()) then
printfn "You are in the temp directory."
else
printfn "You are not in the temp directory."
with e ->
printfn $"The process failed: {e}"
Imports System.IO
Public Class Test
Public Shared Sub Main()
Try
' Get the current directory.
Dim path As String = Directory.GetCurrentDirectory()
Dim target As String = "c:\temp"
Console.WriteLine("The current directory is {0}", path)
If Directory.Exists(target) = False Then
Directory.CreateDirectory(target)
End If
' Change the current directory.
Environment.CurrentDirectory = (target)
If path.Equals(Directory.GetCurrentDirectory()) Then
Console.WriteLine("You are in the temp directory.")
Else
Console.WriteLine("You are not in the temp directory.")
End If
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
Remarques
Le répertoire actif est distinct du répertoire d’origine, c’est-à-dire celui à partir duquel le processus a été démarré.
Pour obtenir la liste des tâches d’E/S courantes, consultez Tâches d’E/S courantes.