Directory.SetCurrentDirectory(String) Méthode

Définition

Définit le répertoire spécifié comme répertoire de travail actuel de l'application.

public:
 static void SetCurrentDirectory(System::String ^ path);
public static void SetCurrentDirectory (string path);
static member SetCurrentDirectory : string -> unit
Public Shared Sub SetCurrentDirectory (path As String)

Paramètres

path
String

Chemin affecté au répertoire de travail actuel.

Exceptions

Une erreur d'E/S s'est produite.

.NET Framework et les versions .NET Core antérieures à 2.1 : path est une chaîne de longueur nulle, ne contient qu’un espace blanc ou contient un ou plusieurs caractères non valides. Vous pouvez rechercher les caractères non valides avec la méthode GetInvalidPathChars().

path a la valeur null.

Le chemin et/ou le nom de fichier spécifiés dépassent la longueur maximale définie par le système.

L’appelant n’a pas l’autorisation nécessaire pour accéder au code non managé.

Le chemin spécifié est introuvable.

Le répertoire spécifié est introuvable.

Exemples

L’exemple suivant montre comment définir le répertoire actif et afficher la racine du répertoire.

// This sample shows how to set the current directory and how to determine
// the root directory.
using namespace System;
using namespace System::IO;
int main()
{
   
   // Create string for a directory. This value should be an existing directory
   // or the sample will throw a DirectoryNotFoundException.
   String^ dir = "C:\\test";
   try
   {
      
      //Set the current directory.
      Directory::SetCurrentDirectory( dir );
   }
   catch ( DirectoryNotFoundException^ e ) 
   {
      Console::WriteLine( "The specified directory does not exist. {0}", e );
   }

   
   // Print to console the results.
   Console::WriteLine( "Root directory: {0}", Directory::GetDirectoryRoot( dir ) );
   Console::WriteLine( "Current directory: {0}", Directory::GetCurrentDirectory() );
}

// The output of this sample depends on what value you assign to the variable dir.
// If the directory c:\test exists, the output for this sample is:
// Root directory: C:\
// Current directory: C:\test
// This sample shows how to set the current directory and how to determine
// the root directory.
using System;
using System.IO;

namespace IOSamples
{
  public class DirectoryRoot
  {
    public static void Main()
    {
    // Create string for a directory. This value should be an existing directory
    // or the sample will throw a DirectoryNotFoundException.
      string dir = @"C:\test";		
      try
      {
          //Set the current directory.
          Directory.SetCurrentDirectory(dir);
      }
      catch (DirectoryNotFoundException e)
      {
          Console.WriteLine("The specified directory does not exist. {0}", e);
      }
    // Print to console the results.
      Console.WriteLine("Root directory: {0}", Directory.GetDirectoryRoot(dir));
      Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory());
    }
  }
}
// The output of this sample depends on what value you assign to the variable dir.
// If the directory c:\test exists, the output for this sample is:
// Root directory: C:\
// Current directory: C:\test
// This sample shows how to set the current directory and how to determine
// the root directory.
open System.IO

// Create string for a directory. This value should be an existing directory
// or the sample will throw a DirectoryNotFoundException.
let dir = @"C:\test"
try
    //Set the current directory.
    Directory.SetCurrentDirectory dir
with :? DirectoryNotFoundException as e ->
    printfn $"The specified directory does not exist. {e}"
    
// Print to console the results.
printfn $"Root directory: {Directory.GetDirectoryRoot dir}"
printfn $"Current directory: {Directory.GetCurrentDirectory()}"
// The output of this sample depends on what value you assign to the variable dir.
// If the directory c:\test exists, the output for this sample is:
// Root directory: C:\
// Current directory: C:\test
' This sample shows how to set the current directory and how to determine
' the root directory.
Imports System.IO

Public Class DirectoryRoot
   
   Public Shared Sub Main()
      ' Create string for a directory. This value should be an existing directory
      ' or the sample will throw a DirectoryNotFoundException.
      Dim dir As String = "C:\test"
      Try
         'Set the current directory.
         Directory.SetCurrentDirectory(dir)
      Catch e As DirectoryNotFoundException
         Console.WriteLine("The specified directory does not exist. {0}", e)
      End Try
      ' Print to console the results.
      Console.WriteLine("Root directory: {0}", Directory.GetDirectoryRoot(dir))
      Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory())
   End Sub
End Class
' The output of this sample depends on what value you assign to the variable dir.
' If the directory c:\test exists, the output for this sample is:
' Root directory: C:\
' Current directory: C:\test

Remarques

Lorsque l’application se termine, le répertoire de travail est restauré à son emplacement d’origine (répertoire où le processus a été démarré).

Le path paramètre est autorisé à spécifier des informations relatives ou absolues sur le chemin d’accès. Les informations relatives au chemin d’accès sont interprétées comme relatives au répertoire de travail actif. Pour obtenir le répertoire de travail actuel, consultez GetCurrentDirectory.

Les espaces de fin sont supprimés de la fin du path paramètre avant de définir le répertoire.

Le path paramètre n’est pas sensible à la casse.

Si vous définissez le répertoire sur un lecteur avec un support amovible (par exemple, « E: » pour un lecteur flash USB), vous pouvez déterminer si le lecteur est prêt à l’aide de la IsReady propriété.

S’applique à

Voir aussi