C # -- traverse folder instances

Source: Internet
Author: User

As I have mentioned in the previous blogs, I won't talk about anything here, so I can directly paste the code.


1. Control interpretation:

The FolderBrowserDialog control is used to display the "Browse folder" dialog box.

TextBox controls a ---- used to display the selected folder

A Button control ---- used to open the "Browse folder" dialog box

ListView controls one ---- used to display the subfolders and file information in the selected folder


2. instance code:

Private void button#click (object sender, EventArgs e) {listView1.Items. clear (); if (folderBrowserDialog1.ShowDialog () = DialogResult. OK) {textBox1.Text = folderBrowserDialog1.SelectedPath; // instantiate the DirectoryInfo object DirectoryInfo dinfo = new DirectoryInfo (textBox1.Text); // obtain all subdirectories and file types in the specified directory. FileSystemInfo [] fsinfos = dinfo. getFileSystemInfos (); foreach (FileSystemInfo fsinfo in fsinfos) {if (fsinfo is DirectoryInfo) // determine whether a folder exists {// use the obtained folder name to instantiate the DirectoryInfo object DirectoryInfo dirinfo = new DirectoryInfo (fsinfo. fullName); // Add the Folder Information listView1.Items for the ListView control. add (dirinfo. name); listView1.Items [listView1.Items. count-1]. subItems. add (dirinfo. fullName); listView1.Items [listView1.Items. count-1]. subItems. add (""); listView1.Items [listView1.Items. count-1]. subItems. add (dirinfo. creationTime. tow.datestring ();} else {// instantiate the FileInfo object FileInfo finfo = new FileInfo (fsinfo. fullName); // Add the file information listView1.Items for the ListView control. add (finfo. name); listView1.Items [listView1.Items. count-1]. subItems. add (finfo. fullName); listView1.Items [listView1.Items. count-1]. subItems. add (finfo. length. toString (); listView1.Items [listView1.Items. count-1]. subItems. add (finfo. creationTime. toShortDateString ());}}}}




3. Run:


Click Browse



After selecting a folder:



Related Article

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.