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 );
}