string[] TPathAry=Directory.GetFiles(tpath);
Directory类的GetFiles方法获得指定文件夹下的所有文件路径,返回一个数组
该数组的length属性就是某个文件架下所有文件的个数。
以下为MSDN的解释定义
[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML xmlns:MSHelp="http://msdn.microsoft.com/mshelp" DIR="LTR"><HEAD>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=gb2312">
<TITLE>GetFiles 方法 (String)</TITLE>
<xml>
<MSHelp:TOCTitle Title="GetFiles 方法 (String)"/>
<MSHelp:RLTitle Title="Directory.GetFiles 方法 (String)"/>
<MSHelp:Keyword Index="A" Term="frlrfSystemIODirectoryClassGetFilesTopic1"/>
<MSHelp:Attr Name="DocSet" Value="NETFramework"/>
<MSHelp:Attr Name="TopicType" Value="kbSyntax"/>
<MSHelp:Attr Name="DevLang" Value="CSharp"/>
<MSHelp:Attr Name="DevLang" Value="VB"/>
<MSHelp:Attr Name="DevLang" Value="C++"/>
<MSHelp:Attr Name="DevLang" Value="JScript"/>
<MSHelp:Attr Name="DevLang" Value="VJ#"/>
<MSHelp:Attr Name="Technology" Value="WFC"/>
<MSHelp:Attr Name="Technology" Value="ManagedC"/>
<MSHelp:Attr Name="TechnologyVers" Value="kbWFC"/>
<MSHelp:Attr Name="TechnologyVers" Value="kbManagedC"/>
<MSHelp:Attr Name="Locale" Value="kbChineseSimp"/>
<MSHelp:Attr Name="DocSet" Value="NETCompactFramework"/>
<MSHelp:Attr Name="TechnologyVers" Value="kbProfile2NETCF"/>
</xml>
<SCRIPT SRC="ms-help:/../commoner/scripts/dtuelink.js"></SCRIPT>
<META name="rtfFileName" content="systemiodirectory_1.rtf"></HEAD>
<body topmargin="0" id="bodyID" class = "dtBODY">
<object id="obj_cook" classid="clsid:59CC0C20-679B-11D2-88BD-0800361A1803" style="display:none;"></object><div id="nsbanner">
<div id="bannerrow1">
<TABLE CLASS="bannerparthead" CELLSPACING=0>
<TR ID="hdr">
<TD CLASS="runninghead" nowrap>.NET Framework 类库</TD>
<TD CLASS="product" nowrap> </TD>
</TR>
</TABLE>
</div>
<div id="TitleRow">
<H1 class="dtH1"><A NAME="frlrfsystemiodirectoryclassgetfilestopic1"></A>Directory.GetFiles 方法 (String)</H1>
</div></div>
<DIV id="nstext" valign="bottom">
<div id="allHistory" class="saveHistory" onsave="saveAll()" onload="loadAll()"></div>
<P>返回指定目录中的文件的名称。</P>
<PRE class="syntax"><SPAN class="lang">[Visual Basic]
</SPAN><B>Overloads Public Shared Function GetFiles( _
ByVal </B><I>path</I><B> As </B><A HREF="frlrfsystemstringclasstopic.htm">String</A><B> _</B>
<B>) As </B><A HREF="frlrfsystemstringclasstopic.htm">String</A><B>()</B>
<SPAN class="lang">[C#]
</SPAN><B>public static </B><A HREF="frlrfsystemstringclasstopic.htm">string</A><B>[] GetFiles(</B>
<B> </B><A HREF="frlrfsystemstringclasstopic.htm">string</A><B> </B><I>path</I>
<B>);</B>
<SPAN class="lang">[C++]
</SPAN><B>public: static </B><A HREF="frlrfsystemstringclasstopic.htm">String</A><B>* GetFiles(</B>
<B> </B><A HREF="frlrfsystemstringclasstopic.htm">String</A><B>* </B><I>path</I>
<B>) __gc[];</B>
<SPAN class="lang">[JScript]
</SPAN><B>public static function GetFiles(
</B><I>path</I><B> : </B><A HREF="frlrfsystemstringclasstopic.htm">String</A>
<B>) : </B><A HREF="frlrfsystemstringclasstopic.htm">String</A><B>[];</B></PRE>
<H4 class="dtH4">参数</H4>
<DL>
<DT><I>path</I></DT>
<DD>将从其检索文件的目录。</dd>
</DL>
<H4 class="dtH4">返回值</H4>
<P>指定目录中文件名的 <B>String</B> 数组。</P>
<H4 class="dtH4">异常</H4>
<div class="tablediv"><table cellspacing="0" class="dtTABLE">
<TR VALIGN="top">
<TH width=50%>异常类型</TH>
<TH width=50%>条件</TH>
</TR>
<TR VALIGN="top">
<TD width=50%><A HREF="frlrfsystemioioexceptionclasstopic.htm">IOException</A></TD>
<TD width=50%><I>path</I> 是一个文件名。</TD>
</TR>
<TR VALIGN="top">
<TD width=50%><A HREF="frlrfsystemunauthorizedaccessexceptionclasstopic.htm">UnauthorizedAccessException</A></TD>
<TD width=50%>调用方没有所要求的权限。</TD>
</TR>
<TR VALIGN="top">
<TD width=50%><A HREF="frlrfsystemargumentexceptionclasstopic.htm">ArgumentException</A></TD>
<TD width=50%><I>path</I> 是一个零长度字符串,仅包含空白或者包含一个或多个由 <A HREF="frlrfsystemiopathclassinvalidpathcharstopic.htm">InvalidPathChars</A> 定义的无效字符。</TD>
</TR>
<TR VALIGN="top">
<TD width=50%><A HREF="frlrfsystemargumentnullexceptionclasstopic.htm">ArgumentNullException</A></TD>
<TD width=50%><I>path</I> 为空引用(Visual Basic 中为 <B>Nothing</B>)。</TD>
</TR>
<TR VALIGN="top">
<TD width=50%><A HREF="frlrfsystemiopathtoolongexceptionclasstopic.htm">PathTooLongException</A></TD>
<TD width=50%>指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。</TD>
</TR>
<TR VALIGN="top">
<TD width=50%><A HREF="frlrfsystemiodirectorynotfoundexceptionclasstopic.htm">DirectoryNotFoundException</A></TD>
<TD width=50%>指定的目录无效,比如在未映射的驱动器上。</TD>
</TR>
</table></div>
<H4 class="dtH4">备注</H4>
<P>此方法与将星号 (*) 指定为搜索模式的 <MSHelp:link keywords="frlrfSystemIODirectoryClassGetFilesTopic2" TABINDEX="0">GetFiles(String, String)</MSHelp:link> 方法相同。</P>
<P>允许 <I>path</I> 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 <A HREF="frlrfsystemiodirectoryclassgetcurrentdirectorytopic.htm">GetCurrentDirectory</A>。</P>
<P><I>path</I> 参数不区分大小写。</P>
<P>有关使用此方法的示例,请参阅下面的“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。</P>
<div class="tablediv"><table cellspacing="0" class="dtTABLE">
<TR VALIGN="top">
<TH width=50%>若要执行此操作...</TH>
<TH width=50%>请参阅本主题中的示例...</TH>
</TR>
<TR VALIGN="top">
<TD width=50%>创建文本文件。</TD>
<TD width=50%><MSHelp:link keywords="cpconwritingtexttofile" TABINDEX="0">向文件写入文本</MSHelp:link> </TD>
</TR>
<TR VALIGN="top">
<TD width=50%>写入文本文件。</TD>
<TD width=50%><MSHelp:link keywords="cpconwritingtexttofile" TABINDEX="0">向文件写入文本</MSHelp:link> </TD>
</TR>
<TR VALIGN="top">
<TD width=50%>读取文本文件。</TD>
<TD width=50%><MSHelp:link keywords="cpconreadingtextfromfile" TABINDEX="0">从文件读取文本</MSHelp:link> </TD>
</TR>
<TR VALIGN="top">
<TD width=50%>复制目录。</TD>
<TD width=50%><A HREF="frlrfsystemiodirectoryclasstopic.htm">Directory</A> </TD>
</TR>
<TR VALIGN="top">
<TD width=50%>重命名或移动目录。</TD>
<TD width=50%><A HREF="frlrfsystemiodirectoryclassmovetopic.htm">Directory.Move</A>
<P><A HREF="frlrfsystemiodirectoryinfoclassmovetotopic.htm">DirectoryInfo.MoveTo</A> </P>
</TD>
</TR>
<TR VALIGN="top">
<TD width=50%>重命名或移动文件。</TD>
<TD width=50%><A HREF="frlrfsystemiofileclassmovetopic.htm">File.Move</A>
<P><A HREF="frlrfsystemiofileinfoclassmovetotopic.htm">FileInfo.MoveTo</A> </P>
</TD>
</TR>
<TR VALIGN="top">
<TD width=50%>删除目录。</TD>
<TD width=50%><A HREF="frlrfsystemiodirectoryclassdeletetopic.htm">Directory.Delete</A>
<P><A HREF="frlrfsystemiodirectoryinfoclassdeletetopic.htm">DirectoryInfo.Delete</A> </P>
</TD>
</TR>
<TR VALIGN="top">
<TD width=50%>创建目录。</TD>
<TD width=50%><A HREF="frlrfsystemiodirectoryclasscreatedirectorytopic.htm">CreateDirectory</A>
<P><A HREF="frlrfsystemiofileinfoclassdirectorytopic.htm">Directory</A> </P>
</TD>
</TR>
<TR VALIGN="top">
<TD width=50%>创建子目录。</TD>
<TD width=50%><A HREF="frlrfsystemiodirectoryinfoclasscreatesubdirectorytopic.htm">CreateSubdirectory</A> </TD>
</TR>
<TR VALIGN="top">
<TD width=50%>查看目录中的文件。</TD>
<TD width=50%><A HREF="frlrfsystemiofileinfoclassnametopic.htm">Name</A> </TD>
</TR>
<TR VALIGN="top">
<TD width=50%>查看目录的子目录。</TD>
<TD width=50%><A HREF="frlrfsystemiodirectoryclassgetdirectoriestopic.htm">GetDirectories</A>
<P><A HREF="frlrfsystemiodirectoryinfoclassgetdirectoriestopic.htm">GetDirectories</A> </P>
</TD>
</TR>
<TR VALIGN="top">
<TD width=50%>查看目录的所有子目录中的所有文件。</TD>
<TD width=50%><A HREF="frlrfsystemiodirectoryinfoclassgetfilesysteminfostopic.htm">GetFileSystemInfos</A> </TD>
</TR>
<TR VALIGN="top">
<TD width=50%>查看目录大小。</TD>
<TD width=50%><A HREF="frlrfsystemiodirectoryclasstopic.htm">Directory</A> </TD>
</TR>
<TR VALIGN="top">
<TD width=50%>确定文件是否存在。</TD>
<TD width=50%><A HREF="frlrfsystemiofileclassexiststopic.htm">Exists</A> </TD>
</TR>
<TR VALIGN="top">
<TD width=50%>确定目录是否存在。</TD>
<TD width=50%><A HREF="frlrfsystemiodirectoryclassexiststopic.htm">Exists</A> </TD>
</TR>
<TR VALIGN="top">
<TD width=50%>获取文件属性。</TD>
<TD width=50%><A HREF="frlrfsystemiofileclassgetattributestopic.htm">GetAttributes</A> </TD>
</TR>
<TR VALIGN="top">
<TD width=50%>设置文件属性。</TD>
<TD width=50%><A HREF="frlrfsystemiofileclasssetattributestopic.htm">SetAttributes</A> </TD>
</TR>
</table></div>
<H4 class="dtH4">示例</H4>
<PRE class="code"><SPAN class="lang">[Visual Basic]</SPAN>
' For Directory.GetFiles and Directory.GetDirectories
' For File.Exists, Directory.Exists
Imports System
Imports System.IO
Imports System.Collections
Public Class RecursiveFileProcessor
Public Overloads Shared Sub Main(ByVal args() As String)
Dim path As String
For Each path In args
If File.Exists(path) Then
' This path is a file.
ProcessFile(path)
Else
If Directory.Exists(path) Then
' This path is a directory.
ProcessDirectory(path)
Else
Console.WriteLine("{0} is not a valid file or directory.", path)
End If
End If
Next path
End Sub 'Main
' Process all files in the directory passed in, recurse on any directories
' that are found, and process the files they contain.
Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
' Process the list of files found in the directory.
Dim fileName As String
For Each fileName In fileEntries
ProcessFile(fileName)
Next fileName
Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
' Recurse into subdirectories of this directory.
Dim subdirectory As String
For Each subdirectory In subdirectoryEntries
ProcessDirectory(subdirectory)
Next subdirectory
End Sub 'ProcessDirectory
' Insert logic for processing found files here.
Public Shared Sub ProcessFile(ByVal path As String)
Console.WriteLine("Processed file '{0}'.", path)
End Sub 'ProcessFile
End Class 'RecursiveFileProcessor
<SPAN class="lang">[C#]</SPAN>
// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;
public class RecursiveFileProcessor
{
public static void Main(string[] args)
{
foreach(string path in args)
{
if(File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
ProcessFile(fileName);
// Recurse into subdirectories of this directory.
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
Console.WriteLine("Processed file '{0}'.", path);
}
}
<SPAN class="lang">[C++]</SPAN>
// For Directory::GetFiles and Directory::GetDirectories
// For File::Exists, Directory::Exists
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
using namespace System::Collections;
// Insert logic for processing found files here.
void ProcessFile(String* path) {
Console::WriteLine(S"Processed file '{0}'.", path);
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
void ProcessDirectory(String* targetDirectory) {
// Process the list of files found in the directory.
String* fileEntries[] = Directory::GetFiles(targetDirectory);
IEnumerator* files = fileEntries->GetEnumerator();
while (files->MoveNext()) {
String* fileName = __try_cast<String*>(files->Current);
ProcessFile(fileName);
}
// Recurse into subdirectories of this directory.
String* subdirectoryEntries[] = Directory::GetDirectories(targetDirectory);
IEnumerator* dirs = subdirectoryEntries->GetEnumerator();
while (dirs->MoveNext()) {
String* subdirectory = __try_cast<String*>(dirs->Current);
ProcessDirectory(subdirectory);
}
}
void main(int argc, char* argv[]) {
for(int i=1; i < argc; i++) {
String* path = argv[i];
if (File::Exists(path)) {
// This path is a file
ProcessFile(path);
} else if (Directory::Exists(path)) {
// This path is a directory
ProcessDirectory(path);
}else {
Console::WriteLine(S"{0} is not a valid file or directory.", path);
}
}
}
<SPAN class="lang">[JScript]</SPAN>
//For Directory.GetFiles and Directory.GetDirectories
import System;
import System.IO;
import System.Collections;
// For File.Exists, Directory.Exists
// Takes an array of file names or directory names on the command line.
// Determines what kind of name it is and processes it appropriately
public class RecursiveFileProcessor {
public static function Main(args : String[]) : void {
for(var i : int in args) {
var path : String = args[i];
if (File.Exists(path)) {
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path)) {
// This path is a directory
ProcessDirectory(path);
}
else {
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}
// Process all files in the directory passed in, and recurse on any directories
// that are found to process the files they contain
public static function ProcessDirectory(targetDirectory : String) : void {
// Process the list of files found in the directory
var fileEntries : String [] = Directory.GetFiles(targetDirectory);
for (var i : int in fileEntries)
ProcessFile(fileEntries[i]);
// Recurse into subdirectories of this directory
var subdirectoryEntries : String[] = Directory.GetDirectories(targetDirectory);
for (i in subdirectoryEntries)
ProcessDirectory(subdirectoryEntries[i]);
}
// Real logic for processing found files would go here.
public static function ProcessFile(path : String) : void {
Console.WriteLine("Processed file '{0}'.", path);
}
}
// For JScript there is no 'Main' routine defined and hence the command line arguments
// have to be obtained with a call to System.Environment.GetCommandLineArgs
RecursiveFileProcessor.Main(System.Environment.GetCommandLineArgs());
</PRE>
<H4 class="dtH4">要求</H4>
<P><b class="le">平台: </b>Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版 - Windows CE .NET, 公共语言基础结构 (CLI) 标准</P>
<P><b class="le">.NET Framework 安全性: </b>
<UL type="disc">
<LI><A HREF="frlrfsystemsecuritypermissionsfileiopermissionclasstopic.htm">FileIOPermission</A>。关联的枚举:<A HREF="frlrfsystemsecuritypermissionsfileiopermissionaccessclasstopic.htm">FileIOPermissionAccess.PathDiscovery</A> </li>
</UL>
<H4 class="dtH4">请参见</H4>
<P><A HREF="frlrfsystemiodirectoryclasstopic.htm">Directory 类</A> | <A HREF="frlrfsystemiodirectorymemberstopic.htm">Directory 成员</A> | <A HREF="frlrfsystemio.htm">System.IO 命名空间</A> | <A HREF="frlrfsystemiodirectoryclassgetfilestopic.htm">Directory.GetFiles 重载列表</A> | <MSHelp:link keywords="cpconworkingwithio" TABINDEX="0">使用 I/O</MSHelp:link> | <MSHelp:link keywords="cpconreadingtextfromfile" TABINDEX="0">从文件读取文本</MSHelp:link> | <MSHelp:link keywords="cpconwritingtexttofile" TABINDEX="0">向文件写入文本</MSHelp:link> <MSHelp:ktable keywords="frlrfSystemIODirectoryClassGetFilesTopic1_ktable;MCPlusPlusLink" disambiguator = span separator=" | " prefix="| " postfix="&nbsp;" indexMoniker="!DefaultAssociativeIndex"/></P>
<DIV CLASS="footer">
<HR>
<p><a href="mailto:netfwsdk@microsoft.com?subject=CHS Documentation Feedback (v1.1): systemiodirectory_1.rtf, GetFiles 方法 (String)">发送有关此主题的意见</a>
<p><MSHelp:link keywords="copybeta_nonconfidential" TABINDEX="0">© 2001-2002 Microsoft Corporation。保留所有权利。</MSHelp:link>
<p>
</div></div>
</BODY>
</HTML>[/CODE]