| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1211 人关注过本帖
标题:[求助]谁知道用C#.net(WEB)中,怎么察看一个文件夹里的文件个数?
只看楼主 加入收藏
mgh1986
Rank: 1
等 级:新手上路
帖 子:152
专家分:0
注 册:2006-12-25
收藏
 问题点数:0 回复次数:4 
[求助]谁知道用C#.net(WEB)中,怎么察看一个文件夹里的文件个数?
我想写个文件读取器,但是文件的个数读取不了 ,哪为老大指点一二?
搜索更多相关主题的帖子: WEB 文件 
2007-01-29 20:40
yms123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
收藏
得分:0 

string[] TPathAry=Directory.GetFiles(tpath);
Directory类的GetFiles方法获得指定文件夹下的所有文件路径,返回一个数组
该数组的length属性就是某个文件架下所有文件的个数。

2007-01-29 21:11
yms123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
收藏
得分:0 

以下为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&nbsp;Framework&nbsp;类库</TD>
<TD CLASS="product" nowrap>&nbsp;</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&nbsp;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&nbsp;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 &lt;mscorlib.dll&gt;
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-&gt;GetEnumerator();
while (files-&gt;MoveNext()) {
String* fileName = __try_cast&lt;String*&gt;(files-&gt;Current);
ProcessFile(fileName);
}
// Recurse into subdirectories of this directory.
String* subdirectoryEntries[] = Directory::GetDirectories(targetDirectory);
IEnumerator* dirs = subdirectoryEntries-&gt;GetEnumerator();
while (dirs-&gt;MoveNext()) {
String* subdirectory = __try_cast&lt;String*&gt;(dirs-&gt;Current);
ProcessDirectory(subdirectory);
}
}
void main(int argc, char* argv[]) {
for(int i=1; i &lt; 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">平台:&nbsp;</b>Windows&nbsp;98, Windows&nbsp;NT&nbsp;4.0, Windows&nbsp;ME, Windows&nbsp;2000, Windows&nbsp;XP&nbsp;Home&nbsp;Edition, Windows&nbsp;XP&nbsp;Professional, Windows&nbsp;Server&nbsp;2003&nbsp;系列, .NET&nbsp;Framework&nbsp;精简版&nbsp;-&nbsp;Windows&nbsp;CE&nbsp;.NET, 公共语言基础结构&nbsp;(CLI)&nbsp;标准</P>
<P><b class="le">.NET Framework 安全性:&nbsp;</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="&amp;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">&copy; 2001-2002 Microsoft Corporation。保留所有权利。</MSHelp:link>
<p>
</div></div>
</BODY>
</HTML>[/CODE]


2007-01-29 21:14
yms123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
收藏
得分:0 
GetFiles的参数是一个字符串型的文件夹的绝对路径(注意是文件夹路径不是文件的路径)
2007-01-29 21:15
mgh1986
Rank: 1
等 级:新手上路
帖 子:152
专家分:0
注 册:2006-12-25
收藏
得分:0 
强 崇拜啊!

2007-01-29 21:46
快速回复:[求助]谁知道用C#.net(WEB)中,怎么察看一个文件夹里的文件个数?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038208 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved