Directory.GetFileSystemEntries メソッド

定義

指定した条件を満たすすべてのファイルとサブディレクトリの名前を返します。

オーバーロード

名前 説明
GetFileSystemEntries(String)

指定したパス内のすべてのファイルとサブディレクトリの名前を返します。

GetFileSystemEntries(String, String)

指定したパス内の検索パターンと一致するファイル名とディレクトリ名の配列を返します。

GetFileSystemEntries(String, String, EnumerationOptions)

指定したパスの検索パターンと列挙オプションに一致するファイル名とディレクトリ名の配列を返します。

GetFileSystemEntries(String, String, SearchOption)

指定したパス内の検索パターンに一致するすべてのファイル名とディレクトリ名の配列を返し、必要に応じてサブディレクトリを検索します。

GetFileSystemEntries(String)

指定したパス内のすべてのファイルとサブディレクトリの名前を返します。

public:
 static cli::array <System::String ^> ^ GetFileSystemEntries(System::String ^ path);
public static string[] GetFileSystemEntries(string path);
static member GetFileSystemEntries : string -> string[]
Public Shared Function GetFileSystemEntries (path As String) As String()

パラメーター

path
String

検索するディレクトリへの相対パスまたは絶対パス。 この文字列では大文字と小文字は区別されません。

返品

String[]

指定したディレクトリ内のファイルとサブディレクトリの名前の配列。ファイルまたはサブディレクトリが見つからない場合は空の配列。

例外

呼び出し元に必要なアクセス許可がありません。

2.1 より前のバージョンの .NET Framework と .NET Core: path は長さ 0 の文字列で、空白のみを含むか、1 つ以上の無効な文字を含みます。 GetInvalidPathChars()を使用して無効な文字を照会できます。

pathnullです。

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

path はファイル名です。

指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。

次の例では、 GetFileSystemEntries メソッドを使用して、文字列の配列に、ユーザー指定の場所にあるすべてのファイルとサブディレクトリの名前を入力し、配列内の各文字列をコンソールに出力します。 この例は、このメソッドに共通するすべてのエラーをキャッチするように構成されています。

using System;

namespace GetFileSystemEntries
{
    class Class1
    {
        static void Main(string[] args)
        {
            Class1 snippets = new Class1();

            string path = System.IO.Directory.GetCurrentDirectory();
            string filter = "*.exe";

            snippets.PrintFileSystemEntries(path);
            snippets.PrintFileSystemEntries(path, filter);		
            snippets.GetLogicalDrives();
            snippets.GetParent(path);
            snippets.Move("C:\\proof", "C:\\Temp");
        }

        void PrintFileSystemEntries(string path)
        {
            
            try
            {
                // Obtain the file system entries in the directory path.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path);

                foreach (string str in directoryEntries)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                System.Console.WriteLine("The path encapsulated in the " +
                    "Directory object does not exist.");
            }
        }
        void PrintFileSystemEntries(string path, string pattern)
        {
            try
            {
                // Obtain the file system entries in the directory
                // path that match the pattern.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path, pattern);

                foreach (string str in directoryEntries)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                System.Console.WriteLine("The path encapsulated in the " +
                    "Directory object does not exist.");
            }
        }

        // Print out all logical drives on the system.
        void GetLogicalDrives()
        {
            try
            {
                string[] drives = System.IO.Directory.GetLogicalDrives();

                foreach (string str in drives)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (System.IO.IOException)
            {
                System.Console.WriteLine("An I/O error occurs.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
        }
        void GetParent(string path)
        {
            try
            {
                System.IO.DirectoryInfo directoryInfo =
                    System.IO.Directory.GetParent(path);

                System.Console.WriteLine(directoryInfo.FullName);
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, or " +
                    "contains invalid characters.");
            }
        }
        void Move(string sourcePath, string destinationPath)
        {
            try
            {
                System.IO.Directory.Move(sourcePath, destinationPath);
                System.Console.WriteLine("The directory move is complete.");
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");	
            }
            catch (System.IO.IOException)
            {
                System.Console.WriteLine("An attempt was made to move a " +
                    "directory to a different " +
                    "volume, or destDirName " +
                    "already exists.");
            }
        }
    }
}
open System
open System.IO
open System.Security

let printFileSystemEntries path =
    try
        // Obtain the file system entries in the directory path.
        let directoryEntries = Directory.GetFileSystemEntries path

        for str in directoryEntries do
            printfn $"{str}"
    with
    | :? ArgumentNullException ->
        printfn "Path is a null reference."
    | :? SecurityException ->
        printfn $"The caller does not have the required permission."
    | :? ArgumentException ->
        printfn $"Path is an empty string, contains only white spaces, or contains invalid characters."
    | :? DirectoryNotFoundException ->
        printfn $"The path encapsulated in the Directory object does not exist."

let printFileSystemEntriesPattern path pattern =
    try
        // Obtain the file system entries in the directory
        // path that match the pattern.
        let directoryEntries = Directory.GetFileSystemEntries(path, pattern)

        for str in directoryEntries do
            printfn $"{str}"
    with
    | :? ArgumentNullException -> printfn "Path is a null reference."
    | :? SecurityException -> printfn "The caller does not have the required permission."
    | :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
    | :? DirectoryNotFoundException -> printfn "The path encapsulated in the Directory object does not exist."

// Print out all logical drives on the system.
let getLogicalDrives () =
    try
        let drives = Directory.GetLogicalDrives()

        for str in drives do
            printfn $"{str}"
    with
    | :? IOException -> printfn "An I/O error occurs."
    | :? SecurityException -> printfn "The caller does not have the required permission."

let getParent path =
    try
        let directoryInfo = Directory.GetParent path
        printfn $"{directoryInfo.FullName}"

    with
    | :? ArgumentNullException -> printfn "Path is a null reference."
    | :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."

let move sourcePath destinationPath =
    try
        Directory.Move(sourcePath, destinationPath)
        printfn "The directory move is complete."

    with
    | :? ArgumentNullException -> printfn "Path is a null reference."
    | :? SecurityException -> printfn "The caller does not have the required permission."
    | :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
    | :? IOException -> printfn "An attempt was made to move a directory to a different volume, or destDirName already exists."

let path = Directory.GetCurrentDirectory()
let filter = "*.exe"

printFileSystemEntries path
printFileSystemEntriesPattern path filter
getLogicalDrives ()
getParent path
move "C:\\proof" "C:\\Temp"
Option Explicit On 
Option Strict On

Namespace GetFileSystemEntries
    Class Class1
        Overloads Shared Sub Main(ByVal args() As String)
            Dim snippets As New Class1()
            Dim path As String = System.IO.Directory.GetCurrentDirectory()
            Dim filter As String = "*.exe"
            snippets.PrintFileSystemEntries(path)
            snippets.PrintFileSystemEntries(path, filter)
            snippets.GetLogicalDrives()
            snippets.GetParent(path)
            snippets.Move("C:\proof", "C:\Temp")
        End Sub

        Sub PrintFileSystemEntries(ByVal path As String)
            Try
                ' Obtain the file system entries in the directory path.
                Dim directoryEntries As String()
                directoryEntries = System.IO.Directory.GetFileSystemEntries(path)
                Dim str As String
                For Each str In directoryEntries
                    System.Console.WriteLine(str)
                Next str
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                        "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.DirectoryNotFoundException
                System.Console.WriteLine("The path encapsulated in the " + _
                                        "Directory object does not exist.")
            End Try
        End Sub
        Sub PrintFileSystemEntries(ByVal path As String, _
                                   ByVal pattern As String)
            Try
                ' Obtain the file system entries in the directory
                ' path that match the pattern.
                Dim directoryEntries As String()
                directoryEntries = _
                   System.IO.Directory.GetFileSystemEntries(path, pattern)

                Dim str As String
                For Each str In directoryEntries
                    System.Console.WriteLine(str)
                Next str
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                        "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.DirectoryNotFoundException
                System.Console.WriteLine("The path encapsulated in the " + _
                                        "Directory object does not exist.")
            End Try
        End Sub

        ' Print out all logical drives on the system.
        Sub GetLogicalDrives()
            Try
                Dim drives As String()
                drives = System.IO.Directory.GetLogicalDrives()

                Dim str As String
                For Each str In drives
                    System.Console.WriteLine(str)
                Next str
            Catch exp As System.IO.IOException
                System.Console.WriteLine("An I/O error occurs.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                           "required permission.")
            End Try
        End Sub
        Sub GetParent(ByVal path As String)
            Try
                Dim directoryInfo As System.IO.DirectoryInfo
                directoryInfo = System.IO.Directory.GetParent(path)
                System.Console.WriteLine(directoryInfo.FullName)
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                     "contains only white spaces, or " + _
                                     "contains invalid characters.")
            End Try
        End Sub
        Sub Move(ByVal sourcePath As String, ByVal destinationPath As String)
            Try
                System.IO.Directory.Move(sourcePath, destinationPath)
                System.Console.WriteLine("The directory move is complete.")
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                           "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.IOException
                System.Console.WriteLine("An attempt was made to move a " + _
                                        "directory to a different " + _
                                        "volume, or destDirName " + _
                                        "already exists.")
            End Try
        End Sub
    End Class
End Namespace

注釈

返されるファイル名とディレクトリ名の順序は保証されません。特定の並べ替え順序が必要な場合は、 Sort メソッドを使用します。

EnumerateFileSystemEntriesメソッドとGetFileSystemEntriesメソッドは次のように異なります。EnumerateFileSystemEntriesを使用すると、コレクション全体が返される前にエントリのコレクションの列挙を開始できます。GetFileSystemEntriesを使用する場合は、配列にアクセスする前に、エントリの配列全体が返されるのを待つ必要があります。 そのため、多くのファイルやディレクトリを操作する場合は、 EnumerateFileSystemEntries の方が効率的です。

このメソッドは、アスタリスク (*) を検索パターンとして指定した GetFileSystemEntries と同じです。

path パラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、 GetCurrentDirectoryを参照してください。

path パラメーターの大文字と小文字の区別は、コードが実行されているファイル システムの大文字と小文字が区別されます。 たとえば、NTFS (既定の Windows ファイル システム) では大文字と小文字が区別され、Linux ファイル システムでは大文字と小文字が区別されます。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

GetFileSystemEntries(String, String)

指定したパス内の検索パターンと一致するファイル名とディレクトリ名の配列を返します。

public:
 static cli::array <System::String ^> ^ GetFileSystemEntries(System::String ^ path, System::String ^ searchPattern);
public static string[] GetFileSystemEntries(string path, string searchPattern);
static member GetFileSystemEntries : string * string -> string[]
Public Shared Function GetFileSystemEntries (path As String, searchPattern As String) As String()

パラメーター

path
String

検索するディレクトリへの相対パスまたは絶対パス。 この文字列では大文字と小文字は区別されません。

searchPattern
String

path内のファイルとディレクトリの名前と照合する検索文字列。 このパラメーターには、有効なリテラル パスとワイルドカード (* および ?) 文字の組み合わせを含めることができますが、正規表現はサポートされていません。

返品

String[]

指定した検索条件に一致するファイル名とディレクトリ名の配列。ファイルまたはディレクトリが見つからない場合は空の配列。

例外

呼び出し元に必要なアクセス許可がありません。

2.1 より前のバージョンの .NET Framework と .NET Core: path は長さ 0 の文字列で、空白のみを含むか、1 つ以上の無効な文字を含みます。 GetInvalidPathChars() メソッドを使用して無効な文字を照会できます。

-または-

searchPattern には有効なパターンが含まれていません。

path または searchPatternnull

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

path はファイル名です。

指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。

次の例では、 GetFileSystemEntries メソッドを使用して、特定の場所でユーザー指定のフィルターに一致するすべてのファイルの名前を文字列の配列に入力し、配列内の各文字列をコンソールに出力します。 この例は、このメソッドに共通するすべてのエラーをキャッチするように構成されています。

using System;

namespace GetFileSystemEntries
{
    class Class1
    {
        static void Main(string[] args)
        {
            Class1 snippets = new Class1();

            string path = System.IO.Directory.GetCurrentDirectory();
            string filter = "*.exe";

            snippets.PrintFileSystemEntries(path);
            snippets.PrintFileSystemEntries(path, filter);		
            snippets.GetLogicalDrives();
            snippets.GetParent(path);
            snippets.Move("C:\\proof", "C:\\Temp");
        }

        void PrintFileSystemEntries(string path)
        {
            
            try
            {
                // Obtain the file system entries in the directory path.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path);

                foreach (string str in directoryEntries)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                System.Console.WriteLine("The path encapsulated in the " +
                    "Directory object does not exist.");
            }
        }
        void PrintFileSystemEntries(string path, string pattern)
        {
            try
            {
                // Obtain the file system entries in the directory
                // path that match the pattern.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path, pattern);

                foreach (string str in directoryEntries)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                System.Console.WriteLine("The path encapsulated in the " +
                    "Directory object does not exist.");
            }
        }

        // Print out all logical drives on the system.
        void GetLogicalDrives()
        {
            try
            {
                string[] drives = System.IO.Directory.GetLogicalDrives();

                foreach (string str in drives)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (System.IO.IOException)
            {
                System.Console.WriteLine("An I/O error occurs.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
        }
        void GetParent(string path)
        {
            try
            {
                System.IO.DirectoryInfo directoryInfo =
                    System.IO.Directory.GetParent(path);

                System.Console.WriteLine(directoryInfo.FullName);
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, or " +
                    "contains invalid characters.");
            }
        }
        void Move(string sourcePath, string destinationPath)
        {
            try
            {
                System.IO.Directory.Move(sourcePath, destinationPath);
                System.Console.WriteLine("The directory move is complete.");
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");	
            }
            catch (System.IO.IOException)
            {
                System.Console.WriteLine("An attempt was made to move a " +
                    "directory to a different " +
                    "volume, or destDirName " +
                    "already exists.");
            }
        }
    }
}
open System
open System.IO
open System.Security

let printFileSystemEntries path =
    try
        // Obtain the file system entries in the directory path.
        let directoryEntries = Directory.GetFileSystemEntries path

        for str in directoryEntries do
            printfn $"{str}"
    with
    | :? ArgumentNullException ->
        printfn "Path is a null reference."
    | :? SecurityException ->
        printfn $"The caller does not have the required permission."
    | :? ArgumentException ->
        printfn $"Path is an empty string, contains only white spaces, or contains invalid characters."
    | :? DirectoryNotFoundException ->
        printfn $"The path encapsulated in the Directory object does not exist."

let printFileSystemEntriesPattern path pattern =
    try
        // Obtain the file system entries in the directory
        // path that match the pattern.
        let directoryEntries = Directory.GetFileSystemEntries(path, pattern)

        for str in directoryEntries do
            printfn $"{str}"
    with
    | :? ArgumentNullException -> printfn "Path is a null reference."
    | :? SecurityException -> printfn "The caller does not have the required permission."
    | :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
    | :? DirectoryNotFoundException -> printfn "The path encapsulated in the Directory object does not exist."

// Print out all logical drives on the system.
let getLogicalDrives () =
    try
        let drives = Directory.GetLogicalDrives()

        for str in drives do
            printfn $"{str}"
    with
    | :? IOException -> printfn "An I/O error occurs."
    | :? SecurityException -> printfn "The caller does not have the required permission."

let getParent path =
    try
        let directoryInfo = Directory.GetParent path
        printfn $"{directoryInfo.FullName}"

    with
    | :? ArgumentNullException -> printfn "Path is a null reference."
    | :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."

let move sourcePath destinationPath =
    try
        Directory.Move(sourcePath, destinationPath)
        printfn "The directory move is complete."

    with
    | :? ArgumentNullException -> printfn "Path is a null reference."
    | :? SecurityException -> printfn "The caller does not have the required permission."
    | :? ArgumentException -> printfn "Path is an empty string, contains only white spaces, or contains invalid characters."
    | :? IOException -> printfn "An attempt was made to move a directory to a different volume, or destDirName already exists."

let path = Directory.GetCurrentDirectory()
let filter = "*.exe"

printFileSystemEntries path
printFileSystemEntriesPattern path filter
getLogicalDrives ()
getParent path
move "C:\\proof" "C:\\Temp"
Option Explicit On 
Option Strict On

Namespace GetFileSystemEntries
    Class Class1
        Overloads Shared Sub Main(ByVal args() As String)
            Dim snippets As New Class1()
            Dim path As String = System.IO.Directory.GetCurrentDirectory()
            Dim filter As String = "*.exe"
            snippets.PrintFileSystemEntries(path)
            snippets.PrintFileSystemEntries(path, filter)
            snippets.GetLogicalDrives()
            snippets.GetParent(path)
            snippets.Move("C:\proof", "C:\Temp")
        End Sub

        Sub PrintFileSystemEntries(ByVal path As String)
            Try
                ' Obtain the file system entries in the directory path.
                Dim directoryEntries As String()
                directoryEntries = System.IO.Directory.GetFileSystemEntries(path)
                Dim str As String
                For Each str In directoryEntries
                    System.Console.WriteLine(str)
                Next str
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                        "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.DirectoryNotFoundException
                System.Console.WriteLine("The path encapsulated in the " + _
                                        "Directory object does not exist.")
            End Try
        End Sub
        Sub PrintFileSystemEntries(ByVal path As String, _
                                   ByVal pattern As String)
            Try
                ' Obtain the file system entries in the directory
                ' path that match the pattern.
                Dim directoryEntries As String()
                directoryEntries = _
                   System.IO.Directory.GetFileSystemEntries(path, pattern)

                Dim str As String
                For Each str In directoryEntries
                    System.Console.WriteLine(str)
                Next str
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                        "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.DirectoryNotFoundException
                System.Console.WriteLine("The path encapsulated in the " + _
                                        "Directory object does not exist.")
            End Try
        End Sub

        ' Print out all logical drives on the system.
        Sub GetLogicalDrives()
            Try
                Dim drives As String()
                drives = System.IO.Directory.GetLogicalDrives()

                Dim str As String
                For Each str In drives
                    System.Console.WriteLine(str)
                Next str
            Catch exp As System.IO.IOException
                System.Console.WriteLine("An I/O error occurs.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                           "required permission.")
            End Try
        End Sub
        Sub GetParent(ByVal path As String)
            Try
                Dim directoryInfo As System.IO.DirectoryInfo
                directoryInfo = System.IO.Directory.GetParent(path)
                System.Console.WriteLine(directoryInfo.FullName)
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                     "contains only white spaces, or " + _
                                     "contains invalid characters.")
            End Try
        End Sub
        Sub Move(ByVal sourcePath As String, ByVal destinationPath As String)
            Try
                System.IO.Directory.Move(sourcePath, destinationPath)
                System.Console.WriteLine("The directory move is complete.")
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                           "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.IOException
                System.Console.WriteLine("An attempt was made to move a " + _
                                        "directory to a different " + _
                                        "volume, or destDirName " + _
                                        "already exists.")
            End Try
        End Sub
    End Class
End Namespace

注釈

返されるファイル名とディレクトリ名の順序は保証されません。特定の並べ替え順序が必要な場合は、 Sort メソッドを使用します。

searchPattern にはリテラル文字とワイルドカード文字の組み合わせを指定できますが、正規表現はサポートされていません。 searchPatternでは、次のワイルドカード指定子を使用できます。

ワイルドカード指定子 一致
* (アスタリスク) その位置の 0 個以上の文字。
? (疑問符) その位置に 1 文字だけ入力します。

ワイルドカード以外の文字はリテラル文字です。 たとえば、 searchPattern 文字列 "*t" は、文字 "t" で終わる path 内のすべての名前を検索します。 searchPattern文字列 "s*" は、文字 "s" で始まるpath内のすべての名前を検索します。

searchPattern は、2 つのピリオド ("..") で終わることはできません。または、2 つのピリオド ("..") の後に DirectorySeparatorChar または AltDirectorySeparatorCharを含めることはできません。また、無効な文字を含めることもできます。 GetInvalidPathChars メソッドを使用して、無効な文字のクエリを実行できます。

Note

"*.txt" などの searchPattern でアスタリスク ワイルドカード文字を使用すると、指定した拡張子の文字数が次のように検索に影響します。

  • 指定した拡張子が 3 文字の場合、指定した拡張子で始まる拡張子を持つファイルが返されます。 たとえば、"*.xls" は "book.xls" と "book.xlsx" の両方を返します。
  • それ以外のすべての場合、メソッドは指定された拡張子と完全に一致するファイルを返します。 たとえば、"*.ai" は "file.ai" を返しますが、"file.aif" は返しません。

疑問符ワイルドカード文字を使用すると、このメソッドは指定されたファイル拡張子に一致するファイルのみを返します。 たとえば、ディレクトリ内に "file1.txt" と "file1.txtother" という 2 つのファイルがある場合、"file?.txt" は最初のファイルのみを返しますが、検索パターン "file*.txt" は両方のファイルを返します。

path パラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、 GetCurrentDirectoryを参照してください。

path パラメーターの大文字と小文字の区別は、コードが実行されているファイル システムの大文字と小文字が区別されます。 たとえば、NTFS (既定の Windows ファイル システム) では大文字と小文字が区別され、Linux ファイル システムでは大文字と小文字が区別されます。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

GetFileSystemEntries(String, String, EnumerationOptions)

指定したパスの検索パターンと列挙オプションに一致するファイル名とディレクトリ名の配列を返します。

public:
 static cli::array <System::String ^> ^ GetFileSystemEntries(System::String ^ path, System::String ^ searchPattern, System::IO::EnumerationOptions ^ enumerationOptions);
public static string[] GetFileSystemEntries(string path, string searchPattern, System.IO.EnumerationOptions enumerationOptions);
static member GetFileSystemEntries : string * string * System.IO.EnumerationOptions -> string[]
Public Shared Function GetFileSystemEntries (path As String, searchPattern As String, enumerationOptions As EnumerationOptions) As String()

パラメーター

path
String

検索するディレクトリへの相対パスまたは絶対パス。 この文字列では大文字と小文字は区別されません。

searchPattern
String

path内のファイルとディレクトリの名前と照合する検索文字列。 このパラメーターには、有効なリテラル文字とワイルドカード文字の組み合わせを含めることができますが、正規表現はサポートされていません。

enumerationOptions
EnumerationOptions

使用する検索と列挙の構成を記述するオブジェクト。

返品

String[]

指定した検索パターンと列挙オプションに一致するファイル名とディレクトリ名の配列。ファイルまたはディレクトリが見つからない場合は空の配列。

例外

呼び出し元に必要なアクセス許可がありません。

2.1 より前のバージョンの .NET Framework と .NET Core: path は長さ 0 の文字列で、空白のみを含むか、1 つ以上の無効な文字を含みます。 GetInvalidPathChars() メソッドを使用して無効な文字を照会できます。

-または-

searchPattern には有効なパターンが含まれていません。

path または searchPatternnull

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

path はファイル名です。

指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。

注釈

返されるファイル名とディレクトリ名の順序は保証されません。特定の並べ替え順序が必要な場合は、 Sort メソッドを使用します。

searchPattern にはリテラル文字とワイルドカード文字の組み合わせを指定できますが、正規表現はサポートされていません。 searchPatternでは、次のワイルドカード指定子を使用できます。

ワイルドカード指定子 一致
* (アスタリスク) その位置の 0 個以上の文字。
? (疑問符) その位置に 1 文字だけ入力します。

ワイルドカード以外の文字はリテラル文字です。 たとえば、 searchPattern 文字列 "*t" は、文字 "t" で終わる path 内のすべての名前を検索します。 searchPattern文字列 "s*" は、文字 "s" で始まるpath内のすべての名前を検索します。

searchPattern は、2 つのピリオド ("..") で終わることはできません。または、2 つのピリオド ("..") の後に DirectorySeparatorChar または AltDirectorySeparatorCharを含めることはできません。また、無効な文字を含めることもできます。 GetInvalidPathChars メソッドを使用して、無効な文字のクエリを実行できます。

Note

"*.txt" などの searchPattern でアスタリスク ワイルドカード文字を使用すると、指定した拡張子の文字数が次のように検索に影響します。

  • 指定した拡張子が 3 文字の場合、指定した拡張子で始まる拡張子を持つファイルが返されます。 たとえば、"*.xls" は "book.xls" と "book.xlsx" の両方を返します。
  • それ以外のすべての場合、メソッドは指定された拡張子と完全に一致するファイルを返します。 たとえば、"*.ai" は "file.ai" を返しますが、"file.aif" は返しません。

疑問符ワイルドカード文字を使用すると、このメソッドは指定されたファイル拡張子に一致するファイルのみを返します。 たとえば、ディレクトリ内に "file1.txt" と "file1.txtother" という 2 つのファイルがある場合、"file?.txt" は最初のファイルのみを返しますが、検索パターン "file*.txt" は両方のファイルを返します。

path パラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、 GetCurrentDirectoryを参照してください。

path パラメーターの大文字と小文字の区別は、コードが実行されているファイル システムの大文字と小文字が区別されます。 たとえば、NTFS (既定の Windows ファイル システム) では大文字と小文字が区別され、Linux ファイル システムでは大文字と小文字が区別されます。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

適用対象

GetFileSystemEntries(String, String, SearchOption)

指定したパス内の検索パターンに一致するすべてのファイル名とディレクトリ名の配列を返し、必要に応じてサブディレクトリを検索します。

public:
 static cli::array <System::String ^> ^ GetFileSystemEntries(System::String ^ path, System::String ^ searchPattern, System::IO::SearchOption searchOption);
public static string[] GetFileSystemEntries(string path, string searchPattern, System.IO.SearchOption searchOption);
static member GetFileSystemEntries : string * string * System.IO.SearchOption -> string[]
Public Shared Function GetFileSystemEntries (path As String, searchPattern As String, searchOption As SearchOption) As String()

パラメーター

path
String

検索するディレクトリへの相対パスまたは絶対パス。 この文字列では大文字と小文字は区別されません。

searchPattern
String

path内のファイルとディレクトリの名前と照合する検索文字列。 このパラメーターには、有効なリテラル パスとワイルドカード (* および ?) 文字の組み合わせを含めることができますが、正規表現はサポートされていません。

searchOption
SearchOption

検索操作に現在のディレクトリのみを含めるか、すべてのサブディレクトリを含めるかを指定する列挙値の 1 つ。 既定値は TopDirectoryOnly です。

返品

String[]

指定した検索条件に一致するファイル名とディレクトリ名の配列。ファイルまたはディレクトリが見つからない場合は空の配列。

例外

.NET Framework および .NET Core バージョンが 2.1 より前の場合: path は長さ 0 の文字列で、空白のみを含むか、無効な文字を含みます。 GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。

-または-

searchPattern には有効なパターンが含まれていません。

pathnullです。

-または-

searchPatternnullです。

searchOption が有効な SearchOption 値ではありません。

path は、マップされていないドライブを参照するなど、無効です。

path はファイル名です。

指定したパス、ファイル名、または組み合わせが、システム定義の最大長を超えています。

呼び出し元に必要なアクセス許可がありません。

呼び出し元に必要なアクセス許可がありません。

注釈

返されるファイル名とディレクトリ名の順序は保証されません。特定の並べ替え順序が必要な場合は、 Sort メソッドを使用します。

searchPattern にはリテラル文字とワイルドカード文字の組み合わせを指定できますが、正規表現はサポートされていません。 searchPatternでは、次のワイルドカード指定子を使用できます。

ワイルドカード指定子 一致
* (アスタリスク) その位置の 0 個以上の文字。
? (疑問符) その位置に 1 文字だけ入力します。

ワイルドカード以外の文字はリテラル文字です。 たとえば、 searchPattern 文字列 "*t" は、文字 "t" で終わる path 内のすべての名前を検索します。 searchPattern文字列 "s*" は、文字 "s" で始まるpath内のすべての名前を検索します。

searchPattern は、2 つのピリオド ("..") で終わることはできません。または、2 つのピリオド ("..") の後に DirectorySeparatorChar または AltDirectorySeparatorCharを含めることはできません。また、無効な文字を含めることもできます。 GetInvalidPathChars メソッドを使用して、無効な文字のクエリを実行できます。

Note

"*.txt" などの searchPattern でアスタリスク ワイルドカード文字を使用すると、指定した拡張子の文字数が次のように検索に影響します。

  • 指定した拡張子が 3 文字の場合、指定した拡張子で始まる拡張子を持つファイルが返されます。 たとえば、"*.xls" は "book.xls" と "book.xlsx" の両方を返します。
  • それ以外のすべての場合、メソッドは指定された拡張子と完全に一致するファイルを返します。 たとえば、"*.ai" は "file.ai" を返しますが、"file.aif" は返しません。

疑問符ワイルドカード文字を使用すると、このメソッドは指定されたファイル拡張子に一致するファイルのみを返します。 たとえば、ディレクトリ内に "file1.txt" と "file1.txtother" という 2 つのファイルがある場合、"file?.txt" は最初のファイルのみを返しますが、検索パターン "file*.txt" は両方のファイルを返します。

EnumerateFileSystemEntriesメソッドとGetFileSystemEntriesメソッドは次のように異なります。EnumerateFileSystemEntriesを使用すると、コレクション全体が返される前にエントリのコレクションの列挙を開始できます。GetFileSystemEntriesを使用する場合は、配列にアクセスする前に、エントリの配列全体が返されるのを待つ必要があります。 そのため、多くのファイルやディレクトリを操作する場合は、 EnumerateFileSystemEntries の方が効率的です。

path パラメーターを使用して相対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。これは、 GetCurrentDirectory メソッドを使用して判断できます。

こちらもご覧ください

適用対象