Three methods to obtain the names of all objects in a specified folder

Source: Internet
Author: User

Returns the path containing the file name, and then truncates the file name.

For example:

String [] filenames = directory. getfiles (path );
For (string files in filenames)
{
Response. write (files. replace (path ,""));
}

Method 2:

Directoryinfo dir = new directoryinfo (@ "c: data ");
Fileinfo [] finfo = dir. getfiles ();
String fnames = string. empty;
For (int I = 0; I <finfo. length; I ++)
{
Fnames + = finfo [I]. name + "<br> ";
}
Response. write (fnames );

Method 3

 

/// <Summary>
/// Obtain the names of all objects in the specified folder
/// </Summary>
/// <Param name = "foldername"> specify the folder name and absolute path. </param>
/// <Param name = "filefilter"> file type filtering, based on the file suffix, such as: *, *. txt, *. xls </param>
/// <Param name = "iscontainsubfolder"> include subfolders </param>
/// <Returns> arraylist array, which is the name of all required file paths </returns>
Public static arraylist getallfilesbyfolder (string foldername, string filefilter, bool iscontainsubfolder)
{
Arraylist resarray = new arraylist ();
String [] files = directory. getfiles (foldername, filefilter );
For (int I = 0; I <files. length; I ++)
{
Resarray. add (files [I]);
}
If (iscontainsubfolder)
{
String [] folders = directory. getdirectories (foldername );
For (int j = 0; j <folders. length; j ++)
{
// Traverse all folders
Arraylist temp = getallfilesbyfolder (folders [j], filefilter, iscontainsubfolder );
Resarray. addrange (temp );
}
}
Return resarray;
}

/// <Summary>
/// Obtain the names of all objects in the specified folder, without filtering the file type
/// </Summary>
/// <Param name = "foldername"> specify the folder name and absolute path. </param>
/// <Param name = "iscontainsubfolder"> include subfolders </param>
/// <Returns> arraylist array, which is the name of all required file paths </returns>
Public static arraylist getallfilesbyfolder (string foldername, bool iscontainsubfolder)
{
Return getallfilesbyfolder (foldername, "*", iscontainsubfolder );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.