Three ways to get all the file names under the specified folder

Source: Internet
Author: User

Returns the path that contains the file name. and then intercept the filename.

Such as:

string[] Filenames=directory.getfiles (path);
For (string files in filenames)
{
Response.Write (Files.replace (Path, ""));
}

Method Two:

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 Three

///<summary>
///Get all file names under the specified folder
///</summary>
///<param name= "foldername" > Specify folder names , absolute path </param>
///<param name= "filefilter" > file type filtering, according to the file suffix name, such as: *,*.txt,*.xls</param>
/// <param name= "Iscontainsubfolder" > whether to include subfolders </param>
///<returns>arraylist Array, for all required file path names < /returns>
public static ArrayList Getallfilesbyfolder (String foldername, String Filefilter,bool Iscontainsubfolder)
{
ArrayList resarray = new ArrayList ();
string[] files = directory.getfiles (foldername, F Ilefilter);
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>
///gets all the file names under the specified folder, does not filter file types
///</summary>
///<param name= "foldername" > Specify folder name, absolute path </param>
///<param name= "Iscontainsubfolder" > whether to include subfolders </param>
///< Returns>arraylist Array, </returns> for all required file path names;
public static ArrayList Getallfilesbyfolder (string FolderName, BOOL Iscontainsubfolder)
{
return Getallfilesbyfolder (foldername, "*", Iscontainsubfolder);
}

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.