C # is very powerful. You can use it to easily make your own file browser. The following describes the general implementation process of the file browser. For more information about the usage of these controls, see the online help of C.
You need to use the following controls:
Treeview (used to display the directory tree );
Listview (used to display file and directory lists );
Splitter (used to allow users to adjust the size of Treeview and listview );
Other functions such as mainmenu, toolbar, statusbar, and imagelist depend on your actual needs.
First, create a new C # project (Windows ApplicationProgram), Name myfileview, name the window mainform, and adjust the size of the main window ). Add mainmenu, toolbar, statusbar, imagelist, and other controls.
Then, add the Treeview control, name it Treeview, set the dock attribute to left, add the splitter control, and set the dock attribute to left. Finally, add the listview control, name it listview, and set the dock property to fill.
After the interface is ready, how can I display folders and files on this interface? We need to addCode.
First, reference the following namespace:
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. IO;
Using system. runtime. interopservices;
Add the following code to the mainform_load event to display the directory tree in the Treeview control:
Private void mainform_load (Object sender, system. eventargs E)
// Obtain the logical drive
String [] logicdrives = system. Io. Directory. getlogicaldrives ();
Treenode [] Croot = new treenode [logicdrives. Length];
For (INT I = 0; I <logicdrives. length; I ++)
{
Treenode drivesnode = new treenode (logicdrives [I]);
Treeview. nodes. Add (drivesnode );
If (logicdrives [I]! = "A: \" & logicdrives [I]! = "B :\\")
Getsubnode (drivesnode, true );
}
}
Here, getsubnode is a method used to obtain sub-directories and create a directory tree node. The parameter: pathname is the obtained sub-directory to create a sub-node under this node. The parameter isend: indicates the end, and true indicates the end.
Private void getsubnode (treenode pathname, bool isend)
{
If (! Isend)
Return; // exit this
Treenode curnode;
Directoryinfo [] subdir;
Directoryinfo curdir = new directoryinfo (pathname. fullpath );
Try
{
Subdir = curdir. getdirectories ();
}
Catch {}
Foreach (directoryinfo D in subdir)
{
Curnode = new treenode (D. Name );
Pathname. nodes. Add (curnode );
Getsubnode (curnode, false );
}
}
When you click the plus sign on the left of the directory node, the node is expanded. In this case, add the following code to the afterexpand event to obtain the sub-directory nodes in this directory:
Private void treeview_afterexpand (Object sender, system. Windows. Forms. treevieweventargs E)
{
Try
{
Foreach (treenode tn in E. node. nodes)
{
If (! Tn. isexpanded)
Getsubnode (TN, true );
}
}
Catch {;}
}
When you click the selected directory node, the listview control on the right should display the files and directories under this directory. The Code is as follows:
Private void treeview_afterselect (Object sender, system. Windows. Forms. treevieweventargs E)
{
Listview. Items. Clear ();
Directoryinfo seldir = new directoryinfo (E. node. fullpath );
Directoryinfo [] listdir;
Fileinfo [] listfile;
Try
{
Listdir = seldir. getdirectories ();
Listfile = seldir. getfiles ();
}
Catch {}
Foreach (directoryinfo D in listdir)
Listview. Items. Add (D. Name, 6 );
Foreach (fileinfo D in listfile)
Listview. Items. Add (D. Name );
}
At this point, a simple file browser is made up. Of course, it is still very elementary. It cannot even be used to open a file. In addition, it cannot display the icons of files and directories, no error handling, no security control ...... It gives you only one idea.