导出 (0) 打印
全部展开

Directory.GetFileSystemEntries 方法 (String, String)

更新:2007 年 11 月

返回与指定搜索条件匹配的文件系统项的数组。

命名空间:  System.IO
程序集:  mscorlib(在 mscorlib.dll 中)

public static string[] GetFileSystemEntries(
	string path,
	string searchPattern
)
public static String[] GetFileSystemEntries(
	String path,
	String searchPattern
)
public static function GetFileSystemEntries(
	path : String, 
	searchPattern : String
) : String[]

参数

path
类型:System.String
要搜索的路径。
searchPattern
类型:System.String
要与 path 中的文件名匹配的搜索字符串。此 searchPattern 参数不能以两个句点(“..”)结束,不能在 DirectorySeparatorCharAltDirectorySeparatorChar 的前面包含两个句点(“..”),也不能包含 InvalidPathChars 中的任何字符。

返回值

类型:System.String[]
一个 String 数组,它包含与搜索条件匹配的文件系统项。

异常条件
UnauthorizedAccessException

调用方没有所要求的权限。

ArgumentException

path 是一个零长度字符串,仅包含空白或者包含一个或多个由 InvalidPathChars 定义的无效字符。

- 或 -

searchPattern 不包含有效模式。

ArgumentNullException

pathsearchPatternnull

PathTooLongException

指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。

IOException

path 是一个文件名。

DirectoryNotFoundException

指定的路径无效(例如,它位于未映射的驱动器上)。

searchPattern 中允许使用以下通配说明符。

通配符

说明

*

零个或多个字符。

?

正好一个字符。

通配说明符以外的字符表示它们自己。例如,searchPattern 字符串“*t”搜索 path 中所有以字母“t”结尾的名称。searchPattern 字符串“s*”搜索 path 中所有以字母“s”开头的名称。

扩展名恰好是三个字符时的 searchPattern 匹配行为与扩展名多于三个字符时不同。恰好为三个字符的 searchPattern 返回扩展名为三个或三个以上字符的文件。一个字符、两个字符或三个以上字符的 searchPattern 只返回扩展名恰好等于该长度的文件。

以下列表显示了 searchPattern 参数的不同长度的行为:

  • “*.abc”返回扩展名为 .abc、.abcd、.abcde、.abcdef 等的文件。

  • “*.abcd”只返回扩展名为 .abcd 的文件。

  • “*.abcde”只返回扩展名为 .abcde 的文件。

  • “*.abcdef”只返回扩展名为 .abcdef 的文件。

允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory

path 参数不区分大小写。

有关通用 I/O 任务的列表,请参见 通用 I/O 任务

下面的示例通过 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."); 
            }
        }
    }
}


package GetFileSystemEntries ;
import System.*;

class Class1
{
    public 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");
    } //main

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

            for (int iCtr = 0; iCtr < directoryEntries.length; iCtr++) {
                String str = directoryEntries[iCtr];
                System.Console.WriteLine(str);
            }
        }
        catch (ArgumentNullException exp) {
            System.Console.WriteLine("Path is a null reference.");
        }
        catch (System.Security.SecurityException exp) {
            System.Console.WriteLine(("The caller does not have the "
                + "required permission."));
        }
        catch (ArgumentException exp) {
            System.Console.WriteLine(("Path is an empty string, "
                + "contains only white spaces, "
                + "or contains invalid characters."));
        }
        catch (System.IO.DirectoryNotFoundException exp) {
            System.Console.WriteLine(("The path encapsulated in the "
                + "Directory object does not exist."));
        }
    } //PrintFileSystemEntries

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

            for (int iCtr = 0; iCtr < directoryEntries.length; iCtr++) {
                String str = directoryEntries[iCtr];
                System.Console.WriteLine(str);
            }
        }
        catch (ArgumentNullException exp) {
            System.Console.WriteLine("Path is a null reference.");
        }
        catch (System.Security.SecurityException exp) {
            System.Console.WriteLine(("The caller does not have the "
                + "required permission."));
        }
        catch (ArgumentException exp) {
            System.Console.WriteLine(("Path is an empty string, "
                + "contains only white spaces, "
                + "or contains invalid characters."));
        }
        catch (System.IO.DirectoryNotFoundException exp) {
            System.Console.WriteLine(("The path encapsulated in the "
                + "Directory object does not exist."));
        }
    } //PrintFileSystemEntries

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

            for (int iCtr = 0; iCtr < drives.length; iCtr++) {
                String str = drives[iCtr];
                System.Console.WriteLine(str);
            }
        }
        catch (System.IO.IOException exp) {
            System.Console.WriteLine("An I/O error occurs.");
        }
        catch (System.Security.SecurityException exp) {
            System.Console.WriteLine(("The caller does not have the "
                + "required permission."));
        }
    } //GetLogicalDrives

    void GetParent(String path)
    {
        try {
            System.IO.DirectoryInfo directoryInfo = 
                System.IO.Directory.GetParent(path);
            System.Console.WriteLine(directoryInfo.get_FullName());
        }
        catch (ArgumentNullException exp) {
            System.Console.WriteLine("Path is a null reference.");
        }
        catch (ArgumentException exp) {
            System.Console.WriteLine(("Path is an empty string, "
                +"contains only white spaces, or contains invalid characters."));
        }
    } //GetParent

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


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

.NET Framework

受以下版本支持:3.5、3.0、2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:3.5、2.0、1.0

XNA Framework

受以下版本支持:2.0、1.0

社区附加资源

添加
显示:
© 2014 Microsoft