DirectoryInfo(String) Costruttore

Definizione

Inizializza una nuova istanza della classe DirectoryInfo nel percorso specificato.

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

Parametri

path
String

Stringa che specifica il percorso nel quale creare l'oggetto DirectoryInfo.

Eccezioni

path è null.

Il chiamante non dispone dell'autorizzazione richiesta.

.NET Framework e versioni di .NET Core precedenti a 2.1: path contiene caratteri non validi, ad esempio ", <, >o |.

Il percorso specificato, il nome file o entrambi superano la lunghezza massima definita dal sistema.

Esempio

Nell'esempio seguente viene usato questo costruttore per creare la directory e la sottodirectory specificati e viene illustrato che non è possibile eliminare una directory contenente sottodirectory.

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 {}
    }
}
open System.IO

// Specify the directories you want to manipulate.
let di1 = DirectoryInfo @"c:\MyDir"
let di2 = DirectoryInfo @"c:\MyDir\temp"

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

    // This operation will not be allowed because there are subdirectories.
    printfn $"I am about to attempt to delete {di1.Name}."
    di1.Delete()
    printfn "The Delete operation was successful, which was unexpected."
with _ ->
    printfn "The Delete operation failed as expected."
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

Commenti

Questo costruttore non verifica se esiste una directory. Questo costruttore è un segnaposto per una stringa usata per accedere al disco nelle operazioni successive.

Il path parametro può essere un nome file, incluso un file in una condivisione UNC (Universal Naming Convention).

Attenzione

Quando si compila un set di caratteri con un'impostazione culturale specifica e si recuperano gli stessi caratteri con un'impostazione culturale diversa, i caratteri potrebbero non essere interpretabili e potrebbero causare la generazione di un'eccezione.

Per un elenco di attività di I/O comuni, vedere Attività di I/O comuni.

Si applica a

Vedi anche