导出 (0) 打印
全部展开
此文章由机器翻译。 将光标移到文章的句子上,以查看原文。 更多信息。
译文
原文

Directory.GetFiles 方法 (String, String)

返回指定目录中与指定的搜索模式匹配的文件的名称(包含它们的路径)。

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

public static string[] GetFiles(
	string path,
	string searchPattern
)

参数

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

返回值

类型:System.String[]
指定目录中与指定的搜索模式匹配的文件的全名(包含它们的路径)的数组。

异常条件
IOException

path 是一个文件名。

- 或 -

发生了网络错误。

UnauthorizedAccessException

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

ArgumentException

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

- 或 -

searchPattern 不包含有效模式。

ArgumentNullException

pathsearchPatternnull

PathTooLongException

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

DirectoryNotFoundException

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

The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

The returned file names are appended to the supplied path parameter.

If there are no files, or no files match the searchPattern parameter, this method returns an empty array.

The order of the returned file names is not guaranteed; use the Sort() method if a specific sort order is required.

The following wildcard specifiers are permitted in searchPattern.

Wildcard character

Description

*

Zero or more characters.

?

Exactly zero or one character.

Characters other than the wildcard specifiers represent themselves. For example, the searchPattern string "*t" searches for all names in path ending with the letter "t". The searchPattern string "s*" searches for all names in path beginning with the letter "s".

说明说明

When using the asterisk wildcard character in a searchPattern, such as "*.txt", the matching behavior when the extension is exactly three characters long is different than when the extension is more or less than three characters long. A searchPattern with a file extension of exactly three characters returns files having an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files having extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, while a search pattern of "file*.txt" returns both files.

说明说明

Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

The path parameter is not case-sensitive.

For a list of common I/O tasks, see 通用 I/O 任务.

The following example counts the number of files that begin with the specified letter.


using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            // Only get files that begin with the letter "c."
            string[] dirs = Directory.GetFiles(@"c:\", "c*");
            Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);
            foreach (string dir in dirs) 
            {
                Console.WriteLine(dir);
            }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}


.NET Framework

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

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)

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

社区附加资源

添加
显示:
© 2014 Microsoft