Asp. NET Design Network hard disk folder implementation _ Practical skills

Source: Internet
Author: User
Tags parent directory
In introducing the concept of "network hard disk", it has been mentioned that each user has its own space on the "Network hard disk". This is done in the following programming: To provide users with a fixed folder in which users can add/remove new folders or files of their own. As shown in Figure 1, all the content (including files and folders) under that folder is listed when the page is first opened. If you want to go to the next folder, you can select the folder and click the Open button to enter. Here's a step-by-step description of how to view the folder's content features.


Figure 1 User main interface

1. Page load

Because the user directory provided by the program is fixed, such as C:\UserDir, and requires that all contents of the folder be displayed after the page has been loaded, you need to do so in Page_Load: first, to determine if the folder exists, if it does not exist, you need to create it first , and then list the contents of the folder, with the code implemented as follows:

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page

if (Page.ispostback==false)
{
currentpath= @ "C:\UserDir\"; Set current directory
if (Directory.Exists (@ "c:\UserDir\") ==false)//If the directory does not exist, create the directory
Directory.CreateDirectory (@ "c:\UserDir\");
Loaddir (Currentpath); Initial Masquerade into catalog
}
}


The Loaddir (string FullPath) method is used to list all the contents of the folder, with the following code:

private void Loaddir (string fullpath)
{
Currentpath=fullpath;
ArrayList values = new ArrayList ();
string [] myfiles,mydirs;
Myfiles = Directory.GetFiles (FullPath); Get all the files in this directory
if (currentpath!=@ "C:\UserDir")//If not the top-level directory, add the "Return to parent directory" option {
Values. ADD ("Return to Parent directory");
}

Values. AddRange (myfiles); Add File
mydirs= directory.getdirectories (FullPath); Get all directories in this directory
Values. AddRange (Mydirs); Add to Catalog
Filelist.datasource=values; Setting up a data source
Filelist.databind (); Binding Data
}

First you define a ArrayList array object that holds all the content (including folder name and filename) in the top-level directory. The Directory.GetFiles () method returns all the file names under the top-level directory with a string array, so you need to define a string class object Myfiles to hold the returned file name; directory.getdirectories () returns all folder names under the top-level directory, and also defines a string array object Mydirs to save them. Once this is done, you can add the myfiles and mydirs arrays to the values object. The last thing to do is add the data source and binding data for the ListBox control object filelist. One thing to note: If the current directory is not a top-level directory, you need to be able to return to the parent directory, for which you need to add the "Return to parent directory" option in FileList.

2. Multi-level catalog view

By using the two pieces of code listed in the previous section, you can complete all the contents of the top-level directory when the page loads. Of course, it is not enough to list the contents of the top-level directory, similar to the Windows operating system, the folder directory on the network hard disk is nested, there is a level two or multilevel folder directory. For this purpose, some processing is necessary so that the user can view the contents of the Multilevel folder directory. A "open" button is provided in the front interface design, and when the user selects the appropriate folder, the button is clicked to view the contents of the folder.

The following code is added for the Open button. When you double-click the button in the design panel, the event is automatically added to the system, and the code reads as follows:

private void Btnopen_click (object sender, System.EventArgs e)
{
if (filelist.selecteditem.text== "Back to Parent Directory")//Return to Superior directory
{
String Parentpath=directory.getparent (Currentpath). ToString ();
Loaddir (Parentpath);
Return
}
else//Open Directory
{
Loaddir (FileList.SelectedItem.Text);
}
}

The program first determines whether the user selected is "Return to superior directory". If so, first return the parent folder name through the Directory.getparent () method, and then call the Loaddir () method to display the contents of the directory, and if the user does not select "Return to parent directory" but a folder name, You can call the Loaddir () method directly, FileList.SelectedItem.Text the selected folder name, as a parameter to the Loaddir () method.
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.