Directory.GetFileSystemEntries Metoda

Definice

Vrátí názvy všech souborů a podadresářů, které splňují zadaná kritéria.

Přetížení

GetFileSystemEntries(String)

Vrátí názvy všech souborů a podadresářů v zadané cestě.

GetFileSystemEntries(String, String)

Vrátí pole názvů souborů a názvů adresářů, které odpovídají vzoru hledání v zadané cestě.

GetFileSystemEntries(String, String, EnumerationOptions)

Vrátí pole názvů souborů a názvů adresářů, které odpovídají vzoru hledání a možnostem výčtu v zadané cestě.

GetFileSystemEntries(String, String, SearchOption)

Vrátí pole všech názvů souborů a názvů adresářů, které odpovídají vzoru hledání v zadané cestě, a volitelně prohledá podadresáře.

GetFileSystemEntries(String)

Zdroj:
Directory.cs
Zdroj:
Directory.cs
Zdroj:
Directory.cs

Vrátí názvy všech souborů a podadresářů v zadané cestě.

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()

Parametry

path
String

Relativní nebo absolutní cesta k adresáři, který chcete vyhledat. Tento řetězec neodlišuje velká a malá písmena.

Návraty

String[]

Pole názvů souborů a podadresářů v zadaném adresáři nebo prázdné pole, pokud nejsou nalezeny žádné soubory nebo podadresáře.

Výjimky

Volající nemá požadované oprávnění.

Verze .NET Framework a .NET Core starší než 2.1: path je řetězec nulové délky, obsahuje pouze prázdné znaky nebo obsahuje jeden nebo více neplatných znaků. Pomocí příkazu můžete zadat dotaz na neplatné znaky GetInvalidPathChars().

path je null.

Zadaná cesta, název souboru nebo obojí překračují maximální délku definovanou systémem.

path je název souboru.

Zadaná cesta je neplatná (například je na nezmapované jednotce).

Příklady

Následující příklad používá metodu GetFileSystemEntries k vyplnění pole řetězců názvy všech souborů a podadresářů v umístění zadaném uživatelem a vytiskne každý řetězec v poli do konzoly. Příklad je nakonfigurovaný tak, aby zachytil všechny chyby společné pro tuto metodu.

using namespace System;
class Class1
{
public:
   void PrintFileSystemEntries( String^ path )
   {
      try
      {
         
         // Obtain the file system entries in the directory path.
         array<String^>^directoryEntries = System::IO::Directory::GetFileSystemEntries( path );
         for ( int i = 0; i < directoryEntries->Length; i++ )
         {
            System::Console::WriteLine( directoryEntries[ i ] );

         }
      }
      catch ( ArgumentNullException^ ) 
      {
         System::Console::WriteLine(  "Path is a null reference." );
      }
      catch ( System::Security::SecurityException^ ) 
      {
         System::Console::WriteLine(  "The caller does not have the \HelloServer'                  required permission." );
      }
      catch ( ArgumentException^ ) 
      {
         System::Console::WriteLine(  "Path is an empty String, \HelloServer'                  contains only white spaces, \HelloServer'                  or contains invalid characters." );
      }
      catch ( System::IO::DirectoryNotFoundException^ ) 
      {
         System::Console::WriteLine(  "The path encapsulated in the \HelloServer'                  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.
         array<String^>^directoryEntries = System::IO::Directory::GetFileSystemEntries( path, pattern );
         for ( int i = 0; i < directoryEntries->Length; i++ )
         {
            System::Console::WriteLine( directoryEntries[ i ] );

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

   }


   // Print out all logical drives on the system.
   void GetLogicalDrives()
   {
      try
      {
         array<String^>^drives = System::IO::Directory::GetLogicalDrives();
         for ( int i = 0; i < drives->Length; i++ )
         {
            System::Console::WriteLine( drives[ i ] );

         }
      }
      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 \HelloServer'                  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, \HelloServer'                  contains only white spaces, or \HelloServer'                  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 \HelloServer'                  required permission." );
      }
      catch ( ArgumentException^ ) 
      {
         System::Console::WriteLine(  "Path is an empty String, \HelloServer'                  contains only white spaces, \HelloServer'                  or contains invalid characters." );
      }
      catch ( System::IO::IOException^ ) 
      {
         System::Console::WriteLine(  "An attempt was made to move a \HelloServer'                  directory to a different \HelloServer'                  volume, or destDirName \HelloServer'                  already exists." );
      }

   }

};

int main()
{
   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" );
   return 0;
}
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

Poznámky

Pořadí vrácených názvů souborů a adresářů není zaručeno. Sort pokud se vyžaduje konkrétní pořadí řazení, použijte metodu .

Metody EnumerateFileSystemEntries a GetFileSystemEntries se liší následujícím způsobem: Při použití EnumerateFileSystemEntriesmůžete spustit výčet kolekce položek před vrácením celé kolekce; při použití GetFileSystemEntriesmusíte počkat na vrácení celé pole položek, abyste měli přístup k poli. Proto při práci s mnoha soubory a adresáři EnumerateFileSystemEntries může být efektivnější.

Tato metoda je identická GetFileSystemEntries s hvězdičkou (*) zadanou jako vzor vyhledávání.

Parametr path může zadat informace o relativní nebo absolutní cestě. Informace o relativní cestě jsou vykládány jako relativní k aktuálnímu pracovnímu adresáři. Informace o získání aktuálního pracovního adresáře najdete v tématu GetCurrentDirectory.

Rozlišování velkých a malých písmen parametru path odpovídá rozlišení systému souborů, na kterém je kód spuštěn. Například nerozlišuje malá a malá písmena v systému souborů NTFS (výchozí systém souborů Windows) a v systémech souborů Linux se rozlišují malá a malá písmena.

Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.

Viz také

Platí pro

GetFileSystemEntries(String, String)

Zdroj:
Directory.cs
Zdroj:
Directory.cs
Zdroj:
Directory.cs

Vrátí pole názvů souborů a názvů adresářů, které odpovídají vzoru hledání v zadané cestě.

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()

Parametry

path
String

Relativní nebo absolutní cesta k adresáři, který chcete vyhledat. Tento řetězec neodlišuje velká a malá písmena.

searchPattern
String

Hledaný řetězec, který se má shodovat s názvy souborů a adresářů v path. Tento parametr může obsahovat kombinaci platných literálových cest a zástupných znaků (* a ?), ale nepodporuje regulární výrazy.

Návraty

String[]

Pole názvů souborů a názvů adresářů, které odpovídají zadaným kritériím hledání, nebo prázdné pole, pokud nejsou nalezeny žádné soubory nebo adresáře.

Výjimky

Volající nemá požadované oprávnění.

Verze .NET Framework a .NET Core starší než 2.1: path je řetězec nulové délky, obsahuje pouze prázdné znaky nebo obsahuje jeden nebo více neplatných znaků. Pomocí metody se můžete dotazovat GetInvalidPathChars() na neplatné znaky.

-nebo-

searchPattern neobsahuje platný vzor.

path nebo searchPattern je null.

Zadaná cesta, název souboru nebo obojí překračují maximální délku definovanou systémem.

path je název souboru.

Zadaná cesta je neplatná (například je na nezmapované jednotce).

Příklady

Následující příklad používá metodu GetFileSystemEntries k vyplnění pole řetězců názvy všech souborů odpovídajících filtru zadanému uživatelem v určitém umístění a vytiskne každý řetězec v poli do konzoly. Příklad je nakonfigurovaný tak, aby zachytil všechny chyby společné pro tuto metodu.

using namespace System;
class Class1
{
public:
   void PrintFileSystemEntries( String^ path )
   {
      try
      {
         
         // Obtain the file system entries in the directory path.
         array<String^>^directoryEntries = System::IO::Directory::GetFileSystemEntries( path );
         for ( int i = 0; i < directoryEntries->Length; i++ )
         {
            System::Console::WriteLine( directoryEntries[ i ] );

         }
      }
      catch ( ArgumentNullException^ ) 
      {
         System::Console::WriteLine(  "Path is a null reference." );
      }
      catch ( System::Security::SecurityException^ ) 
      {
         System::Console::WriteLine(  "The caller does not have the \HelloServer'                  required permission." );
      }
      catch ( ArgumentException^ ) 
      {
         System::Console::WriteLine(  "Path is an empty String, \HelloServer'                  contains only white spaces, \HelloServer'                  or contains invalid characters." );
      }
      catch ( System::IO::DirectoryNotFoundException^ ) 
      {
         System::Console::WriteLine(  "The path encapsulated in the \HelloServer'                  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.
         array<String^>^directoryEntries = System::IO::Directory::GetFileSystemEntries( path, pattern );
         for ( int i = 0; i < directoryEntries->Length; i++ )
         {
            System::Console::WriteLine( directoryEntries[ i ] );

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

   }


   // Print out all logical drives on the system.
   void GetLogicalDrives()
   {
      try
      {
         array<String^>^drives = System::IO::Directory::GetLogicalDrives();
         for ( int i = 0; i < drives->Length; i++ )
         {
            System::Console::WriteLine( drives[ i ] );

         }
      }
      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 \HelloServer'                  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, \HelloServer'                  contains only white spaces, or \HelloServer'                  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 \HelloServer'                  required permission." );
      }
      catch ( ArgumentException^ ) 
      {
         System::Console::WriteLine(  "Path is an empty String, \HelloServer'                  contains only white spaces, \HelloServer'                  or contains invalid characters." );
      }
      catch ( System::IO::IOException^ ) 
      {
         System::Console::WriteLine(  "An attempt was made to move a \HelloServer'                  directory to a different \HelloServer'                  volume, or destDirName \HelloServer'                  already exists." );
      }

   }

};

int main()
{
   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" );
   return 0;
}
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

Poznámky

Pořadí vrácených názvů souborů a adresářů není zaručeno. Sort pokud se vyžaduje konkrétní pořadí řazení, použijte metodu .

searchPattern může být kombinací literálu a zástupných znaků, ale nepodporuje regulární výrazy. V systému jsou povoleny následující specifikátory zástupných searchPatternznaků.

Specifikátor zástupných znaků Shody
* (hvězdička) Na této pozici není žádný nebo více znaků.
? (otazník) Přesně jeden znak v této pozici.

Jiné znaky než zástupný znak jsou literálové znaky. Například searchPattern řetězec "*t" vyhledá všechna jména končící path písmenem "t". Řetězec searchPattern "s*" vyhledá všechna jména začínající path písmenem "s".

searchPattern nesmí končit dvěma tečkami ("..") nebo obsahovat dvě tečky ("..") za nimi DirectorySeparatorChar nebo AltDirectorySeparatorChar, ani nesmí obsahovat neplatné znaky. Pomocí metody se můžete dotazovat na neplatné znaky GetInvalidPathChars .

Poznámka

Pokud použijete zástupný znak hvězdičky například " searchPattern *.txt", počet znaků v zadané příponě ovlivní hledání následujícím způsobem:

  • Pokud je zadaná přípona přesně tři znaky dlouhá, vrátí metoda soubory s příponami, které začínají určenou příponou. Například "*.xls" vrátí "book.xls" i "book.xlsx".
  • Ve všech ostatních případech metoda vrátí soubory, které přesně odpovídají zadané příponě. Například "*.ai" vrátí "file.ai", ale ne "file.aif".

Pokud použijete zástupný znak otazníku, vrátí tato metoda pouze soubory, které odpovídají zadané příponě souboru. Například u dvou souborů, "file1.txt" a "file1.txtother" v adresáři, vrátí vyhledávací vzor "soubor?.txt" jen první soubor, zatímco vzor hledání "file*.txt" vrátí oba soubory.

Parametr path může zadat informace o relativní nebo absolutní cestě. Informace o relativní cestě jsou vykládány jako relativní k aktuálnímu pracovnímu adresáři. Informace o získání aktuálního pracovního adresáře najdete v tématu GetCurrentDirectory.

Rozlišování velkých a malých písmen parametru path odpovídá rozlišení systému souborů, na kterém je kód spuštěn. Například nerozlišuje malá a malá písmena v systému souborů NTFS (výchozí systém souborů Windows) a v systémech souborů Linux se rozlišují malá a malá písmena.

Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.

Viz také

Platí pro

GetFileSystemEntries(String, String, EnumerationOptions)

Zdroj:
Directory.cs
Zdroj:
Directory.cs
Zdroj:
Directory.cs

Vrátí pole názvů souborů a názvů adresářů, které odpovídají vzoru hledání a možnostem výčtu v zadané cestě.

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()

Parametry

path
String

Relativní nebo absolutní cesta k adresáři, který chcete vyhledat. Tento řetězec neodlišuje velká a malá písmena.

searchPattern
String

Hledaný řetězec, který se má shodovat s názvy podadresářů v path. Tento parametr může obsahovat kombinaci platných literálů a zástupných znaků, ale nepodporuje regulární výrazy.

enumerationOptions
EnumerationOptions

Objekt, který popisuje konfiguraci vyhledávání a výčtu, která se má použít.

Návraty

String[]

Pole názvů souborů a názvů adresářů, které odpovídají zadanému vzoru hledání a možnostem výčtu, nebo prázdné pole, pokud nejsou nalezeny žádné soubory nebo adresáře.

Výjimky

Volající nemá požadované oprávnění.

Verze .NET Framework a .NET Core starší než 2.1: path je řetězec nulové délky, obsahuje pouze prázdné znaky nebo obsahuje jeden nebo více neplatných znaků. Pomocí metody se můžete dotazovat GetInvalidPathChars() na neplatné znaky.

-nebo-

searchPattern neobsahuje platný vzor.

path nebo searchPattern je null.

Zadaná cesta, název souboru nebo obojí překračují maximální délku definovanou systémem.

path je název souboru.

Zadaná cesta je neplatná (například je na nezmapované jednotce).

Poznámky

Pořadí vrácených názvů souborů a adresářů není zaručeno. Sort pokud se vyžaduje konkrétní pořadí řazení, použijte metodu .

searchPattern může být kombinací literálů a zástupných znaků, ale nepodporuje regulární výrazy. V systému jsou povoleny následující specifikátory zástupných searchPatternznaků.

Specifikátor zástupných znaků Shody
* (hvězdička) Na této pozici není žádný nebo více znaků.
? (otazník) Přesně jeden znak na této pozici.

Jiné znaky než zástupný znak jsou literální znaky. Například searchPattern řetězec "*t" vyhledá všechna jména path končící písmenem "t". Řetězec searchPattern "s*" vyhledá všechna jména path začínající písmenem "s".

searchPattern nesmí končit dvěma tečkami (..) nebo obsahovat dvě tečky (..) následované tečkami DirectorySeparatorChar nebo AltDirectorySeparatorChara nesmí obsahovat žádné neplatné znaky. Na neplatné znaky se můžete dotazovat pomocí metody .GetInvalidPathChars

Poznámka

Pokud použijete zástupný znak hvězdička v searchPattern například "*.txt", počet znaků v zadané příponě ovlivní hledání následujícím způsobem:

  • Pokud je zadaná přípona přesně tři znaky dlouhá, vrátí metoda soubory s příponami, které začínají zadanou příponou. Například "*.xls" vrátí "book.xls" i "book.xlsx".
  • Ve všech ostatních případech metoda vrátí soubory, které přesně odpovídají zadané příponě. Například "*.ai" vrátí "file.ai", ale ne "file.aif".

Pokud použijete zástupný znak otazníku, vrátí tato metoda pouze soubory, které odpovídají zadané příponě souboru. Například při použití dvou souborů , "file1.txt" a "file1.txtother" v adresáři, vrátí vyhledávací vzor "soubor?.txt" jenom první soubor, zatímco vzorec hledání "file*.txt" vrátí oba soubory.

Parametr path může zadat relativní nebo absolutní informace o cestě. Informace o relativní cestě jsou vykládány jako relativní k aktuálnímu pracovnímu adresáři. Pokud chcete získat aktuální pracovní adresář, přečtěte si téma GetCurrentDirectory.

Rozlišování velkých a malých písmen parametru path odpovídá systému souborů, na kterém je kód spuštěn. Například v systému souborů NTFS (výchozí systém souborů Windows) se nerozlišují malá a velká písmena a v systémech souborů Linux.

Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.

Platí pro

GetFileSystemEntries(String, String, SearchOption)

Zdroj:
Directory.cs
Zdroj:
Directory.cs
Zdroj:
Directory.cs

Vrátí pole všech názvů souborů a názvů adresářů, které odpovídají vzoru hledání v zadané cestě, a volitelně prohledá podadresáře.

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()

Parametry

path
String

Relativní nebo absolutní cesta k adresáři, který chcete vyhledat. Tento řetězec neodlišuje velká a malá písmena.

searchPattern
String

Hledaný řetězec, který se má shodovat s názvy souborů a adresářů v path. Tento parametr může obsahovat kombinaci platné literálové cesty a zástupných znaků (* a ?), ale nepodporuje regulární výrazy.

searchOption
SearchOption

Jedna z hodnot výčtu, která určuje, zda má operace hledání obsahovat pouze aktuální adresář, nebo by měla obsahovat všechny podadresáře. Výchozí hodnota je TopDirectoryOnly.

Návraty

String[]

Pole souborů s názvy souborů a názvy adresářů, které odpovídají zadaným kritériím hledání, nebo prázdné pole, pokud nejsou nalezeny žádné soubory nebo adresáře.

Výjimky

.NET Framework a .NET Core verze starší než 2.1: path je řetězec nulové délky, obsahuje pouze prázdné znaky nebo obsahuje neplatné znaky. Na neplatné znaky se můžete dotazovat pomocí metody .GetInvalidPathChars()

-nebo-

searchPattern neobsahuje platný vzor.

path je null.

-nebo-

searchPattern je null.

searchOption není platná SearchOption hodnota.

path je neplatný, například odkaz na nenamapovanou jednotku.

path je název souboru.

Zadaná cesta, název souboru nebo kombinace překračují maximální délku definovanou systémem.

Volající nemá požadované oprávnění.

Volající nemá požadované oprávnění.

Poznámky

Pořadí vrácených názvů souborů a adresářů není zaručeno. Sort Pokud se vyžaduje konkrétní pořadí řazení, použijte metodu .

searchPattern může být kombinací literálů a zástupných znaků, ale nepodporuje regulární výrazy. V systému jsou povoleny následující specifikátory zástupných searchPatternznaků.

Specifikátor zástupných znaků Shody
* (hvězdička) Na této pozici není žádný nebo více znaků.
? (otazník) Přesně jeden znak na této pozici.

Jiné znaky než zástupný znak jsou literální znaky. Například searchPattern řetězec "*t" vyhledá všechna jména path končící písmenem "t". Řetězec searchPattern "s*" vyhledá všechna jména path začínající písmenem "s".

searchPattern nesmí končit dvěma tečkami (..) nebo obsahovat dvě tečky (..) následované tečkami DirectorySeparatorChar nebo AltDirectorySeparatorChara nesmí obsahovat žádné neplatné znaky. Na neplatné znaky se můžete dotazovat pomocí metody .GetInvalidPathChars

Poznámka

Pokud použijete zástupný znak hvězdička v searchPattern například "*.txt", počet znaků v zadané příponě ovlivní hledání následujícím způsobem:

  • Pokud je zadaná přípona přesně tři znaky dlouhá, vrátí metoda soubory s příponami, které začínají zadanou příponou. Například "*.xls" vrátí "book.xls" i "book.xlsx".
  • Ve všech ostatních případech metoda vrátí soubory, které přesně odpovídají zadané příponě. Například "*.ai" vrátí "file.ai", ale ne "file.aif".

Pokud použijete zástupný znak otazníku, vrátí tato metoda pouze soubory, které odpovídají zadané příponě souboru. Například při použití dvou souborů , "file1.txt" a "file1.txtother" v adresáři, vrátí vyhledávací vzor "soubor?.txt" jenom první soubor, zatímco vzorec hledání "file*.txt" vrátí oba soubory.

EnumerateFileSystemEntries Metody a GetFileSystemEntries se liší takto: Pokud použijete EnumerateFileSystemEntries, můžete začít s vytvářením výčtu kolekce položek před vrácením celé kolekce; při použití GetFileSystemEntriespříkazu musíte počkat na vrácení celého pole položek, abyste měli přístup k matici. Proto, když pracujete s mnoha soubory a adresáři, EnumerateFileSystemEntries může být efektivnější.

Pomocí parametru můžete zadat relativní informace o cestě path . Informace o relativní cestě jsou interpretovány jako relativní vzhledem k aktuálnímu pracovnímu adresáři, který můžete určit pomocí GetCurrentDirectory metody .

Viz také

Platí pro