How C # Gets the files and subdirectories contained in the specified directory

Source: Internet
Author: User

  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 ...)

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.