Monday, 25 July 2011

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.

Imports System.ID

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

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>&nbsp;
<asp:TextBox ID="txtPath" runat="server"></asp:TextBox>&nbsp;
<br />
<br />
<asp:Button ID="btnOk" runat="server" OnClick="btnOk_Click" Text="Ok" Width="91px" /></fieldset>

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 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

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment