Bagikan melalui


DirectoryInfo(String) Konstruktor

Definisi

Menginisialisasi instans DirectoryInfo baru kelas pada jalur yang ditentukan.

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

Parameter

path
String

String yang menentukan jalur untuk membuat DirectoryInfo.

Pengecualian

pathadalah null.

Pemanggil tidak memiliki izin yang diperlukan.

.NET Framework dan versi .NET Core yang lebih lama dari 2.1: path berisi karakter yang tidak valid seperti ", <, >, atau |.

Jalur yang ditentukan, nama file, atau keduanya melebihi panjang maksimum yang ditentukan sistem.

Contoh

Contoh berikut menggunakan konstruktor ini untuk membuat direktori dan subdirektori yang ditentukan, dan menunjukkan bahwa direktori yang berisi subdirektori tidak dapat dihapus.

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

Keterangan

Konstruktor ini tidak memeriksa apakah ada direktori. Konstruktor ini adalah tempat penampung untuk string yang digunakan untuk mengakses disk dalam operasi berikutnya.

Parameter path dapat berupa nama file, termasuk file pada berbagi Universal Naming Convention (UNC).

Perhatian

Saat Anda mengkompilasi sekumpulan karakter dengan pengaturan budaya tertentu dan mengambil karakter yang sama dengan pengaturan budaya yang berbeda, karakter mungkin tidak dapat ditafsirkan, dan dapat menyebabkan pengecualian dilemparkan.

Untuk daftar tugas I/O umum, lihat Tugas I/O Umum.

Berlaku untuk

Lihat juga