Compartir a través de


DirectoryInfo(String) Constructor

Definición

Inicializa una nueva instancia de la clase DirectoryInfo en la ruta de acceso especificada.

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

Parámetros

path
String

Cadena que especifica la ruta de acceso en la que se va a crear DirectoryInfo.

Excepciones

path es null.

El llamador no dispone del permiso requerido.

Versiones de .NET Framework y .NET Core anteriores a la 2.1: path contienen caracteres no válidos como ", <, >o |.

La ruta de acceso especificada, el nombre de archivo o ambos superan la longitud máxima definida por el sistema.

Ejemplos

En el ejemplo siguiente se usa este constructor para crear el directorio y subdirectorio especificados, y se muestra que no se puede eliminar un directorio que contiene subdirectorios.

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

Comentarios

Este constructor no comprueba si existe un directorio. Este constructor es un marcador de posición para una cadena que se usa para acceder al disco en operaciones posteriores.

El path parámetro puede ser un nombre de archivo, incluido un archivo en un recurso compartido de convención de nomenclatura universal (UNC).

Precaución

Al compilar un conjunto de caracteres con una configuración cultural determinada y recuperar esos mismos caracteres con una configuración cultural diferente, es posible que los caracteres no se puedan interpretar y podrían producir una excepción.

Para obtener una lista de las tareas de E/S comunes, consulte Tareas de E/S comunes.

Se aplica a

Consulte también