How to get the files and subdirectories contained in the specified directory
1. Directoryinfo.getfiles (): Gets the file in the directory (without subdirectories), returns a type of fileinfo[], supports wildcard lookup,
2. Directoryinfo.getdirectories (): Gets a subdirectory of the directory (without subdirectories), returns a type of directoryinfo[], supports wildcard lookup,
3. DirectoryInfo. Getfilesysteminfos (): Gets the files and subdirectories under the specified directory (without subdirectories), the return type is filesysteminfo[], wildcard lookup is supported, and
How to get the basic information for the specified file;
Fileinfo.exists: Gets whether the specified file exists;
Fileinfo.name,fileinfo.extensioin: Gets the name and extension of the file;
Fileinfo.fullname: Gets the fully qualified name (full path) of the file,
Fileinfo.directory: Gets the directory where the file is located, and the return type is DirectoryInfo;
Fileinfo.directoryname: Gets the path (full path) of the directory where the file resides;
Fileinfo.length: Gets the size of the file (in bytes);
Fileinfo.isreadonly: Gets whether the file is read-only;
Fileinfo.attributes: Gets or sets the properties of the specified file, the return type is a FileAttributes enumeration, which can be a combination of multiple values
Fileinfo.creationtime, Fileinfo.lastaccesstime, Fileinfo.lastwritetime: To get the file creation time, access time, modified time, respectively,
--------------------------------- The
Traverse Folder
folder is a tree structure with traversal algorithms that are breadth-and depth-priority.
Difference: Breadth First look for the same level of directory, depth first traversing a branch; With this distinction, you can determine the insertion position of the list, that is, the breadth traversal insertion point is always at the end; The depth traversal insertion point is in the header. Look at the code in detail.
public static void Searchfile (String path, list<object> resultlist) {
File File = new file (path);
if (file.isdirectory ()) {
Linkedlist<file[]> levellinked = new linkedlist<file[]> ();
Levellinked.add (File.listfiles ());
Do {
file[] childfiles = levellinked.remove (0),
for (File cf:childfiles) {
if (cf.isdirectory ()) {
This controls the direction of traversal
Levellinked.add (Cf.listfiles ());//breadth
//levellinked.add (0, Cf.listfiles ());/Depth
} else {
string filename = Cf.getname ();//file name, with suffix
string filePath = Cf.getabsolutepath ();//absolute path
String fileparent = cf.ge Tparent ();//upper path, note final '/'
//comparison algorithm, locate and put in set
string[] sf = {fileName, fileparent, filePath};
Resultlist.add (SF );
}
}
while (Levellinked.size () > 0);
}
}
Add some of the tool classes provided by the API to help you.
1. Multiple suffix judgments, such as. Java,. class,. js, and so on
string[] Hz = {JAVA,CLASS,JS};
Arrays.sort (Hz);//The following search needs to be sorted, for example, the order of class in Java, if not sort (), the class file will not be looked up
Arrays.binarysearch (Hz, fn_);//fn_ file suffix
The recursive algorithm is also a good choice if the hierarchy of the directory is straightforward.
personally feel that the search for files should be a separate thread.
////////////////////////
Recursively traverses all files in a folder and in a child file.
The public void FindFile (string dirpath)//Parameter dirpath is the specified directory
{
Find files in the specified directory and subdirectories, listing subdirectories and files in ListBox1
DirectoryInfo dir=new DirectoryInfo (Dirpath);
Try
{
foreach (DirectoryInfo D in dir.getdirectories ()/Lookup subdirectory
{
FindFile (dir+d.tostring () + "");
LISTBOX1.ITEMS.ADD (dir+d.tostring () + ""); ListBox1 fill in the directory name
}
foreach (FileInfo f in Dir.getfiles ("*.---"))//Find File
{
LISTBOX1.ITEMS.ADD (Dir+f.tostring ()); ListBox1 the filename
}
}
catch (Exception e)
{
MessageBox.Show (E.message);
}
}
Use the following code to restrict the type of file:
foreach (FileInfo f in Dir.getfiles ("*.---"))//Find File
"*.---" refers to the extension of the type of file to be accessed
///////////////////////////////
The code will be better if you change it as follows
DirectoryInfo thefolder=new DirectoryInfo (folderfullname);
directoryinfo[] Dirinfo = Thefolder.getdirectories ();
Traverse Folder
foreach (DirectoryInfo nextfolder in Dirinfo)
THIS.LISTBOX1.ITEMS.ADD (Nextfolder.name);
fileinfo[] FileInfo = Thefolder.getfiles ();
Traverse file
foreach (FileInfo Next ...)