Asp. NET: Directory Creation and deletion

Source: Internet
Author: User
Tags empty
Asp.net| to create an information management system based on B/s structure, generally have more technical requirements than the ordinary website, such as OA system. These systems often need to direct the operation of the system files, such as file directory browsing and additions and deletions operations, file additions and deletions operations. Asp. NET provides programmers with a wealth of file manipulation interfaces that enable them to efficiently implement the functionality of the requirements based on the interface provided.

Asp. NET file operation mainly applies several classes in the System.IO namespace: Directory class, File class, StreamReader class and so on. We know that in the Web-style system file operation, like the data operation of the database, before the file operation, you must ensure that the necessary file operation permissions.

10.1 operation of the directory

10.1.1 Directory creation and deletion

There are several ways to manage files or directories using the ASP.net program, such as Directory.createdirectory,directoryinfo.create to create a directory where we use the Createdircetory method of the directory class to create A directory.

Use Visual Studio.NET 2003来 to create a Web application named Filescon, make a new form, name it dircon_sample1.aspx, and add two button controls. Dircon_sample1.aspx main HTML code is as follows:

<form id= "Form1" method= "POST" runat= "Server" >
<font face= "Song Body" >
<asp:button id= "Button1"
runat= "Server"
text= "New TestFolder directory"
Width= "120px" height= "32px" >
</asp:Button>
<asp:button id= "Button2"
runat= "Server"
text= "Delete TestFolder directory"
Width= "113px" height= "32px" >
</asp:Button>
</FONT>
</form>
The logical code in DirCon_Sample1.aspx.vb is as follows:
'-------Code begin-----
Imports System.IO
Imports System.IO.Directory
Imports System.Web.UI
Public Class WebForm1
Inherits System.Web.UI.Page
#Region the code generated by the Web Forms Designer
' The code generated by the form designer is omitted here
#End Region
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
End Sub
' New Catalog procedure
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
' Declare directory Class
Dim Dircre as System.IO.Directory
' Declare the path of the file directory, and change the path to the physical absolute path
Dim dir as String = Server.MapPath (".") + "\testfolder\"
Try
' Determine if the file directory exists
If not dircre. Exists (dir) Then
' Create the directory using the CreateDirectory method of the Directory class
Dircre. CreateDirectory (dir)
' Pop-up dialog box, prompt to create success!
Response.Write (' <script> alert (' TestFolder directory has been successfully created! ');</script> ")
Else
Response.Write (' <script> alert (' TestFolder directory already exists! ');</script> ")
End If
Catch ex as Exception
Response.Write ("program execution, information described below:<br>" & Ex. Message)
End Try

End Sub
' Delete directory procedure
Private Sub button2_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button2.click
' Declare directory Class
Dim Dircre as System.IO.Directory
' Declare the path of the file directory, and change the path to the physical absolute path
Dim dir as String = Server.MapPath (".") + "\testfolder\"
Try
' Determine if the file directory exists
If Dircre. Exists (dir) Then
' Remove the directory using the CreateDirectory method of the Directory class
The Delete method of the directory class can only delete empty directories, and when there are files in the directory, the deletion will be wrong!
Dircre. Delete (dir)
' Dircre. CreateDirectory (dir)
' Pop-up dialog box, prompt to create success!
Response.Write (' <script> alert (' TestFolder directory has been successfully deleted! ');</script> ")
Else
Response.Write (' <script> alert (' TestFolder directory does not exist! ');</script> ")
End If
Catch ex as Exception
Response.Write ("program execution, information described below:<br>" & Ex. Message)
End Try
End Sub
End Class
'-------Code End-----

After you save the compilation, the results of the dircon_sample1.aspx run as shown in Figure 10.1.


Figure 10.1

When the button "new TestFolder directory" is clicked, in the Filescon directory, a new directory named "TestFolder" is created, and after clicking the button "Delete TestFolder directory", if "TestFolder" is not empty, You can implement the deletion of the directory.



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.