ASP.NET design folder implementation of network hard drive
Author:Eve Cole
Update Time:2009-06-30 15:39:42
As mentioned when introducing the concept of "network hard drive", each user has his or her own space on the "network hard drive". This is how it is handled in the following program design: a fixed folder is provided for the user, under which the user can add/delete new folders or files. As shown in Figure 1, when you open the web page for the first time, all contents under the folder (including files and folders) will be listed. If you want to enter the next-level folder, you can select the folder and click the "Open" button to enter. The implementation of the function of viewing folder contents will be explained step by step below.
Figure 1 User main interface |
1. Page loading
Since the user directory provided by the program is fixed, such as c:UserDir, and all contents of the folder are required to be displayed after the page is loaded, corresponding operations need to be performed in Page_Load: first determine whether the folder exists, if not If it exists, you need to create it first; then list the contents under the folder. The code is implemented as follows:
private void Page_Load(object sender, System.EventArgs e) { //Put user code here to initialize the page
if(Page.IsPostBack==false) { CurrentPath= @"c:UserDir"; //Set 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 loading directory} }
|
The LoadDir(string FullPath) method is used to list all the contents 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); //Get all files in the directory if(CurrentPath!=@"c:UserDir") //If it is not the top directory, add the "return to the upper directory" option { values.Add("Return to the upper directory"); }
values.AddRange(MyFiles); //Add files MyDirs= Directory.GetDirectories(FullPath); //Get all directories in this directory values.AddRange(MyDirs); //Add directories FileList.DataSource=values; //Set data source FileList.DataBind(); //Bind data } |
First, define an ArrayList array object values to store all the contents of the top-level directory (including folder names and file names). The Directory.GetFiles() method returns all file names in the top-level directory. Its return type is a string array, so a string class object MyFiles needs to be defined to save the returned file names; Directory.GetDirectories() returns all files in the top-level directory. Folder names, also define a string array object MyDirs to save them. After completing this, you can add the MyFiles and MyDirs arrays to the values object. The last thing to do is to add a data source and bind data to the ListBox control object FileList. One thing needs to be explained: if the current directory is not the top-level directory, you need to be able to return to the upper-level directory. To do this, you need to add the "Return to upper-level directory" option to the FileList.
2. Multi-level directory viewing
With the two pieces of code listed in the previous section, you can list all the content in the top-level directory when the page loads. Of course, listing the contents of the top-level directory is not enough. Similar to the Windows operating system, the folder directories in the network hard disk are also nested, and there are two or more levels of folder directories. For this purpose, some corresponding processing must be performed so that users can view the contents of multi-level folders. The previous interface design provides an "Open" button. After the user selects the corresponding folder, click the button to view the contents of the folder.
Now add the code for the "Open" button. Double-click the button in the "Design" panel, and the system will automatically add an event to it. The code content is as follows:
private void btnOpen_Click(object sender, System.EventArgs e) { if(FileList.SelectedItem.Text=="Return to the upper-level directory") //Return to the upper-level 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 "Return to the upper directory". If so, you must first return the upper-level folder name through the Directory.GetParent() method, and then call the LoadDir() method to display the contents of the directory; if the user selects not "Return to the upper-level directory" but a folder name, you can directly call the LoadDir() method. FileList.SelectedItem.Text is the selected folder name, which is used as a parameter of the LoadDir() method.