As mentioned in the introduction to the concept of "Network hard disk", each user has his own space on the "Network hard disk. Below Program In the design, a fixed folder is provided for users. Under this folder, users can add/delete new folders or files by themselves. 1. When you open a webpage for the first time, all contents (including files and folders) in the folder are listed ). To enter the next level folder, select the folder and click open. The following describes the steps for viewing the folder content.
Figure 1 main user interface |
1. Page Loading
Because the user directories provided by the program are fixed, such as c: \ userdir, and all contents of the folder must be displayed after the page is loaded, you need to perform the following operations in page_load: first, you need to determine whether the folder exists. If it does not exist, you need to create it first, and then list the content in the folder.CodeThe implementation is as follows:
Private void page_load (Object sender, system. eventargs E) { // Place user code here to initialize the pageIf (page. ispostback = false) { Currentpath = @ "C: \ userdir \"; // you can specify the current directory. If (directory. exists (@ "C: \ userdir \") = false) // If the directory does not exist, create the Directory Directory. createdirectory (@ "C: \ userdir \"); Loaddir (currentpath); // initialize the Mount directory } } |
The loaddir (string fullpath) method is used to list all content in the folder. The Code is as follows:
private void loaddir (string fullpath) {< br> currentpath = fullpath; arraylist values = new arraylist (); string [] myfiles, mydirs; myfiles = directory. getfiles (fullpath); // obtain all files in the directory If (currentpath! = @ "C: \ userdir") // if it is not a top-level directory, add the "back to parent directory" option { values. add ("back to parent directory"); }< P> values. addrange (myfiles); // Add file mydirs = directory. getdirectories (fullpath); // obtain all the directories in this directory values. addrange (mydirs); // Add to the directory filelist. datasource = values; // set the data source filelist. databind (); // bind data } |
First, define an arraylist array object values to store all contents (including folder names and file names) in the top-level directory ). Directory. the getfiles () method returns all file names in the top-level directory. The return type is a string array. Therefore, you need to define a string class Object myfiles to save the returned file name. directory. getdirectories () returns the names of all folders in the top-level directory, and defines a string array object mydirs to save them. After completing these steps, you can add the myfiles and mydirs arrays to the values object. The last thing to do is to add data sources and bind data to The ListBox control object filelist. Note: If the current directory is not a top-level directory, you must be able to return to the parent directory. Therefore, you must add the "back to parent directory" option in filelist.
2. Multi-Level directory viewing
By using the two sections of Code listed in the previous section, you can list all the content in the top-level directory during page loading. Of course, listing the content under the top-level directory is still not enough. Similar to the Windows operating system, folder directories in the network hard disk are also nested, and there are two-level or multi-level folder directories. To solve this problem, you can view the contents of multi-level folders. A "open" button is provided in the previous interface design. After you select a folder, click the button to view the content in the folder.
The following code is added for the "open" button. Double-click the button on the "design" Panel. The system automatically adds events to it. The Code is as follows:
Private void btnopen_click (Object sender, system. eventargs E) { If (filelist. selecteditem. Text = "back to parent directory") // return to parent directory { String parentpath = directory. getparent (currentpath). tostring (); Loaddir (parentpath ); Return; } Else // open the Directory { Loaddir (filelist. selecteditem. Text ); } } |
The program first checks whether the user selected "returns the parent directory ". If yes, you must first use directory. the getparent () method returns the name of the parent folder, and then calls the loaddir () method to display the content in the directory. If the user selects a folder name instead of the "back to parent directory, you can directly call the loaddir () method, filelist. selecteditem. text is the name of the selected folder and is used as a parameter of the loaddir () method.