Use the Treeview and listview controls to view local disk folders and files (winform)

Source: Internet
Author: User
Use the Treeview and listview controls to view local disk folders and files
Put the Treeview, listview, splitter, and imagelist controls on the page, add two small images to imagelist, and set
The Treeview control's checkboxex = true, dock = left, imagelist = imagelist1 (Control name) imageindex = 1, selectedimageindex = 0 (the values of these two attributes are determined by the image Index added to your imagelist control ),
Showlines = false;
Code As follows:

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. IO;

Namespace Node
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. Treeview treeview1;
Private system. Windows. Forms. imagelist imagelist1;
Private system. Windows. Forms. splitter splitter1;
Private system. Windows. Forms. listview listview1;
Private system. componentmodel. icontainer components;

Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();

//
// Todo: add Any constructor code after initializecomponent calls
//
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. components = new system. componentmodel. Container ();
System. Resources. ResourceManager resources = new system. Resources. ResourceManager (typeof (form1 ));
This. treeview1 = new system. Windows. Forms. Treeview ();
This. imagelist1 = new system. Windows. Forms. imagelist (this. components );
This. splitter1 = new system. Windows. Forms. Splitter ();
This. listview1 = new system. Windows. Forms. listview ();
This. suspendlayout ();
//
// Treeview1
//
This. treeview1.checkboxes = true;
This. treeview1.dock = system. Windows. Forms. dockstyle. Left;
This. treeview1.imageindex = 1;
This. treeview1.imagelist = This. imagelist1;
This. treeview1.location = new system. Drawing. Point (0, 0 );
This. treeview1.name = "treeview1 ";
This. treeview1.showlines = false;
This. treeview1.size = new system. Drawing. Size (152,301 );
This. treeview1.tabindex = 0;
This. treeview1.aftercheck + = new system. Windows. Forms. treevieweventhandler (this. treeview=aftercheck );
This. treeview1.afterselect + = new system. Windows. Forms. treevieweventhandler (this. treeview=afterselect );
//
// Imagelist1
//
This. imagelist1.imagesize = new system. Drawing. Size (16, 16 );
This. imagelist1.imagestream = (system. Windows. Forms. imageliststreamer) (resources. GetObject ("imagelist1.imagestream ")));
This. imagelist1.transparentcolor = system. Drawing. color. transparent;
//
// Splitter1
//
This. splitter1.location = new system. Drawing. Point (152, 0 );
This. splitter1.name = "splitter1 ";
This. splitter1.size = new system. Drawing. Size (3,301 );
This. splitter1.tabindex = 1;
This. splitter1.tabstop = false;
//
// Listview1
//
This. listview1.dock = system. Windows. Forms. dockstyle. Fill;
This. listview1.location = new system. Drawing. Point (155, 0 );
This. listview1.name = "listview1 ";
This. listview1.size = new system. Drawing. Size (413,301 );
This. listview1.smallimagelist = This. imagelist1;
This. listview1.tabindex = 2;
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (568,301 );
This. Controls. Add (this. listview1 );
This. Controls. Add (this. splitter1 );
This. Controls. Add (this. treeview1 );
This. Name = "form1 ";
This. Text = "using the Treeview and listview controls to view local disk folders and files ";
This. Load + = new system. eventhandler (this. form#load );
This. resumelayout (false );

}
# Endregion

/// <Summary>
/// ApplicationProgram.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}

Private void form1_load (Object sender, system. eventargs E)
{
String [] drives = directory. getlogicaldrives (); // get the drive on the local machine
Foreach (string drive in drives) // Loop
{
Mynode Mn = new mynode (drive, true );
This. treeview1.nodes. Add (Mn); // Add the drive name to the Treeview control.
}

This. listview1.columns. Add ("name", this. listview1.width/4, horizontalalignment. center );
This. listview1.columns. Add ("size", this. listview1.width/4, horizontalalignment. center );
This. listview1.columns. Add ("type", this. listview1.width/4, horizontalalignment. center );
This. listview1.columns. Add ("modification time", this. listview1.width/4, horizontalalignment. center );
This. listview1.view = view. details;
}

Private void treeviewappsafterselect (Object sender, system. Windows. Forms. treevieweventargs e) // After the Treeview is selected
{
Try
{
Mynode Mn = (mynode) E. node;
If (Mn. isloadfiles = false & Mn. nodes. Count = 0)
{
Directoryinfo di = new directoryinfo (Mn. mypath );
This. listview1.items. Clear ();
Foreach (directoryinfo D in Di. getdirectories () // if it is a folder
{
Mn. nodes. Add (New mynode (D. fullname, false ));
Listviewitem LVI = This. listview1.items. Add (D. Name );
LVI. subitems. Add ("");
LVI. subitems. Add ("folder ");
LVI. subitems. Add (D. lastaccesstime. tostring ());
LVI. imageindex = 1; // set the icon
}
Foreach (fileinfo F in Di. getfiles () // if the file is
{

Listviewitem LVI = This. listview1.items. Add (F. Name, 0); // load the file name and icon
LVI. subitems. Add (F. length. tostring ());
LVI. subitems. Add ("file ");
LVI. subitems. Add (F. lastaccesstime. tostring ());
}
}
}
Catch
{
}

}

Private void treeviewappsaftercheck (Object sender, system. Windows. Forms. treevieweventargs E)
{
// Treenode tnx = E. node; // obtain the selected tree node
// Foreach (treenode t in tnx. nodes) // loop the subnode under this node
//{
// T. Checked = E. node. Checked; // select all child nodes
//}

// You can test either of the above two segments.

bool ischecked = true; // define a bool value
treenode parentnode = E. node. parent; // obtain the parent node of the selected node
If (parentnode = NULL) // if no parent node exists, return
return;
treenode Tn = parentnode. firstnode; // get a node of the parent node
while (TN! = NULL) // if not empty
{< br> If (TN. checked = false) // if this node is not selected
{< br> ischecked = false; // Boolean is false
break; // jump out
}< br> Tn = tn. nextnode; // move to the next node
}< br> parentnode. checked = ischecked; // determine whether the parent node is selected based on the bool value
}< BR >}

public class mynode: treenode // a class that inherits treenode
{< br> private string mytext = NULL;
Public bool isloadfiles = false;
Public mynode (string text, bool isroot)
{< br> mytext = text;
If (isroot) // execute the following command to find the local drive:
{< br> base. TEXT = text. substring (0, text. lastindexof ("\")-1 );
}< br> else // here, the command is executed to find local folders and files.
{< br> base. TEXT = text. substring (text. lastindexof ("\") + 1 );
}< BR >}< br> Public String mypath
{< br> Get
{< br> return mytext;
}< BR >}

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.