DirectoryInfo(String) Constructeur

Définition

Initialise une nouvelle instance de la classe DirectoryInfo sur le chemin d'accès spécifié.Initializes a new instance of the DirectoryInfo class on the specified path.

public:
 DirectoryInfo(System::String ^ path);
public DirectoryInfo (string path);
new System.IO.DirectoryInfo : string -> System.IO.DirectoryInfo
Public Sub New (path As String)

Paramètres

path
String

Chaîne spécifiant le chemin d'accès dans lequel créer DirectoryInfo.A string specifying the path on which to create the DirectoryInfo.

Exceptions

path a la valeur null.path is null.

L'appelant n'a pas l'autorisation requise.The caller does not have the required permission.

path contient des caractères non valides tels que ", <, > ou |.path contains invalid characters such as ", <, >, or |.

Le chemin et/ou le nom de fichier spécifiés dépassent la longueur maximale définie par le système.The specified path, file name, or both exceed the system-defined maximum length.

Exemples

L’exemple suivant utilise ce constructeur pour créer le répertoire et le sous-répertoire spécifiés, et montre qu’un répertoire qui contient des sous-répertoires ne peut pas être supprimé.The following example uses this constructor to create the specified directory and subdirectory, and demonstrates that a directory that contains subdirectories cannot be deleted.

using namespace System;
using namespace System::IO;
int main()
{
   
   // Specify the directories you want to manipulate.
   DirectoryInfo^ di1 = gcnew DirectoryInfo( "c:\\MyDir" );
   DirectoryInfo^ di2 = gcnew DirectoryInfo( "c:\\MyDir\\temp" );
   try
   {
      
      // Create the directories.
      di1->Create();
      di2->Create();
      
      // This operation will not be allowed because there are subdirectories.
      Console::WriteLine( "I am about to attempt to delete {0}.", di1->Name );
      di1->Delete();
      Console::WriteLine( "The Delete operation was successful, which was unexpected." );
   }
   catch ( Exception^ ) 
   {
      Console::WriteLine( "The Delete operation failed as expected." );
   }

}

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        // Specify the directories you want to manipulate.
        DirectoryInfo di1 = new DirectoryInfo(@"c:\MyDir");
        DirectoryInfo di2 = new DirectoryInfo(@"c:\MyDir\temp");

        try
        {
            // Create the directories.
            di1.Create();
            di2.Create();

            // This operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}.", di1.Name);
            di1.Delete();
            Console.WriteLine("The Delete operation was successful, which was unexpected.");
        }
        catch (Exception)
        {
            Console.WriteLine("The Delete operation failed as expected.");
        }
        finally {}
    }
}
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di1 As DirectoryInfo = New DirectoryInfo("c:\MyDir")
        Dim di2 As DirectoryInfo = New DirectoryInfo("c:\MyDir\temp")
        Try
            ' Create the directories.
            di1.Create()
            di2.Create()
            ' This operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}.", di1.Name)
            di1.Delete()
            Console.WriteLine("The Delete operation was successful, which was unexpected.")
        Catch e As Exception
            Console.WriteLine("The Delete operation failed as expected.")
        End Try
    End Sub
End Class

Remarques

Ce constructeur ne vérifie pas si un répertoire existe.This constructor does not check if a directory exists. Ce constructeur est un espace réservé pour une chaîne utilisée pour accéder au disque dans les opérations suivantes.This constructor is a placeholder for a string that is used to access the disk in subsequent operations.

Le path paramètre peut être un nom de fichier, y compris un fichier sur un partage UNC (Universal Naming Convention).The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

Attention

Quand vous compilez un jeu de caractères avec un paramètre culturel particulier et que vous récupérez ces mêmes caractères avec un autre paramètre culturel, les caractères peuvent ne pas être interprétés et peuvent provoquer la levée d’une exception.When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

Pour obtenir la liste des tâches d’e/s courantes, consultez tâches d’e/s courantes.For a list of common I/O tasks, see Common I/O Tasks.

S’applique à

Voir aussi