Delete all files using ASP.NET and VB 2005
See more tutorials in Files. This post has Comments Off on Delete all files using ASP.NET and VB 2005.
This tutorial will show you how to delete all files under a folder.
This tutorial will show you how to delete all files under a folder. First, import the namespace of System.IO. System.IO namespace containing the type allowing the read-write document and the data stream and provides the type that the basic document and the catalogue support.
The Button1_Click event is to perform the delete all file under a folder. We using GetDirectories to returns the subdirectories of the current directory, using GetFiles to returns all files of the current directory. And we can delete all file and directory with looping.
0 1 2 |
Imports System.IO |
The Button1_Click event is to perform the delete all file under a folder. We using GetDirectories to returns the subdirectories of the current directory, using GetFiles to returns all files of the current directory. And we can delete all file and directory with looping.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Partial Class _Default Inherits System.Web.UI.Page Protected Sub btnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim Dire() As DirectoryInfo Dim file() As FileInfo Dim i As Integer If txtPath.Text <> "" Then Dim dir As New DirectoryInfo(txtPath.Text) Dire = dir.GetDirectories() file = dir.GetFiles() If Dire.Length > 0 Then For i = 0 To Dire.Length - 1 Dire(i).Delete(True) Next End If If file.Length > 0 Then For i = 0 To file.Length - 1 file(i).Delete() Next End If End If End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub End Class |
The front end Default.aspx page looks something like this:
0 1 2 3 4 5 6 7 8 9 10 |
<fieldset style="margin-left: 100px; margin-right: 100px"> <legend> Deleting all File </legend> <asp:Label ID="lblInput" runat="server" Text="Please Choose Path:" Width="130px"></asp:Label> <asp:TextBox ID="txtPath" runat="server"></asp:TextBox> <br /> <br /> <asp:Button ID="btnOk" runat="server" OnClick="btnOk_Click" Text="Ok" Width="91px" /></fieldset> |
The flow for the code behind page is as follows.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Imports System.IO Partial Class _Default Inherits System.Web.UI.Page Protected Sub btnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim Dire() As DirectoryInfo Dim file() As FileInfo Dim i As Integer If txtPath.Text <> "" Then Dim dir As New DirectoryInfo(txtPath.Text) Dire = dir.GetDirectories() file = dir.GetFiles() If Dire.Length > 0 Then For i = 0 To Dire.Length - 1 Dire(i).Delete(True) Next End If If file.Length > 0 Then For i = 0 To file.Length - 1 file(i).Delete() Next End If End If End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub End Class |