ASP. NET design network hard disk folder implementation

Source: Internet
Author: User

As mentioned in the introduction to the concept of "Network hard disk", each user has his own space on the "Network hard disk. In the following program 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 must determine whether the folder exists. If it does not exist, you must first create it. Then, you can list the content in the folder. The code implementation is as follows:

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

If (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)
{
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 ");
}

Values. AddRange (MyFiles); // Add a file
MyDirs = Directory. GetDirectories (FullPath); // obtain all directories in the Directory
Values. AddRange (MyDirs); // Add the Directory
FileList. DataSource = values; // sets 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.

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.