
Navigator : Home > Tutorials > File Tutorials > ...
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.
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 |
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
The front end Default.aspx page looks something like this:
<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.
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 |
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
Looking for more ASP.NET Tutorials? Click Here!
Looking for more ASP.NET Tutorials? Click Here!