返回当前目录中的文件名列表
VB.NET语法:
''' -----------------------------------------------------------------------------
''' <summary>
''' 返回当前目录中与给定 sSearchPattern(如“*.txt”)匹配的文件名列表
''' 如果sSearchPattern为空,则返回当前目录中所有的文件名列表
''' </summary>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
''' [Administrator] 2004-9-25 小牛哥 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Shared Function ListFiles(ByVal sPhysicalDirectoryPath As String, Optional ByVal sSearchPattern As String = "") As IList
Dim oList As IList
Dim oDirectoryInfo As IO.DirectoryInfo
Dim oFileInfo As IO.FileInfo
Dim oFileInfoCollection As IO.FileInfo()
![](/Images/OutliningIndicators/InBlock.gif)
Try
If System.IO.Directory.Exists(sPhysicalDirectoryPath) = False Then
Return Nothing
End If
![](/Images/OutliningIndicators/InBlock.gif)
oDirectoryInfo = New IO.DirectoryInfo(sPhysicalDirectoryPath)
If Len(Trim(sSearchPattern)) > 0 Then
oFileInfoCollection = oDirectoryInfo.GetFiles(sSearchPattern)
Else
oFileInfoCollection = oDirectoryInfo.GetFiles()
End If
![](/Images/OutliningIndicators/InBlock.gif)
For Each oFileInfo In oFileInfoCollection
oList.Add(oFileInfo.Name)
Next
![](/Images/OutliningIndicators/InBlock.gif)
Return oList
![](/Images/OutliningIndicators/InBlock.gif)
Catch ex As Exception
'Common.Exception.Append(ex)
Return Nothing
End Try
![](/Images/OutliningIndicators/InBlock.gif)
End Function
C#语法:
/// <summary>
/// 返回当前目录中与给定 sSearchPattern(如“*.txt”)匹配的文件名列表
/// 如果sSearchPattern为空,则返回当前目录中所有的文件名列表
/// </summary>
/// <param name="sPhysicalDirectoryPath">目录绝对路径</param>
/// <returns></returns>
public static IList ListFiles(string sPhysicalDirectoryPath)
{
return ListFiles(sPhysicalDirectoryPath, "");
}
![](/Images/OutliningIndicators/None.gif)
/// <summary>
/// 返回当前目录中与给定 sSearchPattern(如“*.txt”)匹配的文件名列表
/// 如果sSearchPattern为空,则返回当前目录中所有的文件名列表
/// </summary>
/// <param name="sPhysicalDirectoryPath">目录绝对路径</param>
/// <param name="sSearchPattern">搜索字符串(如“*.txt”)</param>
/// <returns></returns>
public static IList ListFiles(string sPhysicalDirectoryPath, string sSearchPattern)
{
IList oList = null;
System.IO.DirectoryInfo oDirectoryInfo;
System.IO.FileInfo[] oFileInfoCollection;
try
{
if (System.IO.Directory.Exists(sPhysicalDirectoryPath) == false)
{
return null;
}
oDirectoryInfo = new System.IO.DirectoryInfo(sPhysicalDirectoryPath);
if (Microsoft.VisualBasic.Strings.Len(Microsoft.VisualBasic.Strings.Trim(sSearchPattern)) > 0)
{
oFileInfoCollection = oDirectoryInfo.GetFiles(sSearchPattern);
}
else
{
oFileInfoCollection = oDirectoryInfo.GetFiles();
}
foreach (System.IO.FileInfo oFileInfo in oFileInfoCollection)
{
oList.Add(oFileInfo.Name);
}
![](/Images/OutliningIndicators/InBlock.gif)
return oList;
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.Message);
return null;
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
C#语法:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
其中C#代码用到了Microsoft.VisualBasic,需要引用Microsoft.VisualBasic.dll文件,如下图:
希望对您设计程序有所帮助:);
要是有人约稿的话,可能会多写些文字,比如这个方法怎么用还需要例子,具体实现的细节都需要有个描述:)。