Read all the file information in the folder using recursive Methods

Source: Internet
Author: User

To read all the file information in a folder, you only need to use a recursive method.

The showeveryfile method in the Custom class is used to read every file in the given folder.

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. Windows. forms;
Namespace showfile
{
Class showfilecls
{
Public static void showerveryfile (string strfolderpath, listview lstview) // defines static functions.
{
Try
{
Directoryinfo di = new directoryinfo (strfolderpath); // define the folder as directoryinfo
Filesysteminfo [] fsinfo = Di. getfilesysteminfos (); // gets information about files and folders in this folder, and returns an array in filesysteminfo format.
Foreach (filesysteminfo FS in fsinfo) // traverses the Array
{
If (FS is fileinfo) // if a file is traversed
{
Lstview. Items. Add (FS. fullname );
Lstview. items [lstview. items. count-1]. subitems. add (FS. name. remove (FS. name. lastindexof ('. '), FS. extension. length ));
Lstview. items [lstview. Items. Count-1]. subitems. Add (FS. Extension );
Lstview. items [lstview. Items. Count-1]. subitems. Add (FS. creationtime. to1_datestring ());
}
If (FS is directoryinfo) // If the retrieved folder is
{
Showerveryfile (FS. fullname, lstview); // Recursive Method
}
}
}
Catch (exception ex)
{
MessageBox. Show (ex. message, "tip ");
}
}
}
}

After defining this class, you can add code to the Click Event of the button.

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Namespace showfile
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Public static bool boolsort = true; // Global static variable, used to set the sorting method when clicking a column
Private void form1_load (Object sender, eventargs E)
{
Listview1.view = view. Details; // display details. This sentence is very important !!!
Listview1.allowcolumnreorder = true; // The order in which columns can be arranged
Listview1.fullrowselect = true; // you can select the entire row.
Listview1.gridlines = true; // sets the gridline.
Listview1.sorting = sortorder. ascending; // you can specify the order as the forward order.

// Set attribute values for the column
Listview1.columns. Add ("file path", 100, horizontalalignment. Left );
Listview1.columns. Add ("file name", 100, horizontalalignment. Left );
Listview1.columns. Add ("File Format", 100, horizontalalignment. Left );
Listview1.columns. Add ("creation time", 100, horizontalalignment. Left );
}

Private void btnbrowse_click (Object sender, eventargs E)
{
String strpath = ""; // file path
If (folderbrowserdialog1.showdialog () = dialogresult. OK) // if you select a path and click OK
{
Listview1.items. Clear (); // first clear the content in the list
Strpath = folderbrowserdialog1.selectedpath; // obtain the selected file path
Txtpath. Text = strpath; // display the file path in textbox.
Showfilecls. showerveryfile (strpath, listview1); // The class does not need to be instantiated because it is defined as a static function.
}
}

Private void listview1_columnclick (Object sender, columnclickeventargs E)
{
Boolsort =! Boolsort; // reverse this variable so that each click is sorted in a different way
If (boolsort)
{
Listview1.sorting = sortorder. ascending; // forward
}
Else
{
Listview1.sorting = sortorder. Descending; // reverse
}
}
}
}
Final generation Effect

The figure shows that the system not only displays the files in the root directory of the mathtype folder, but also the files in the fonts and postscript folders of the Sun folder.

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.