Deleting files from server is a common process that web developers perform every day. Thanks to the .net framework the process is fairly easy when you delete single file. Things go little complicated when you try to delete selected multiple files from a server folder.
We will use an example here of a scenario that you have a media library web site where the lessons is categorized. . We design the database to contain two tables the first “categories” and the other is “lessons”. The lessons table contains a field called “mp3filelocation” that contains the url of the mp3 of the lesson. The “categories” and the “lessons” is related in a one to many relation. You should set the “delete role” in the relation properties window of the vwd data base diagram to “cascade” . This means when you delete a category all the related lessons will be deleted automatically from the data base but you have to delete the mp3file from it’s directory from the code behind page.
In our scenario the website admin should have the option of deleting a single lesson also to delete a category with all the related lessons.
Deleting single file:
In the “lesson_delete” page we need to delete the mp3 related to the lesson we are deleting. To accomplish this we will put the code to delete the related file in the “SqlDataSource1_Deleted” event handler. we will simply add a databound asp.net label that is bounded to mp3fileurl database field then in the code behind take the file path and delete it.
Dim LABELfileurl As Label = DirectCast(FormView2.FindControl(“mp3nameLabel”),Label)
Dim filenamemp3 As String = LABELfileurl.Text
If e.AffectedRows >0 Then
If filenamemp3.Length >3
1-we used “formview2.findcontrol” function to find the asp.net label we want inside the formview(notice form view is naming container so you cant access the control inside it directly like usual ).
2-we used “directcast” because “findcontrol” function return an undefined control so to define the returning object as a label we use “directcast” function.
3-then we store the mp3file name in a variable .
4-check if the data base row successfully removed using “e.affectedrows>0”
5-then check if there is a mp3file related to the removed lessons.
6-then use the “file.delete” to delete the mp3file this method accept the physical path of the file to delete it we use “server.mappath” to return the physical path .
7-and you finished.
Multiple files deleting:
Thing will be a little complicated here in our scenario we have a group of mp3 files located in a directory called “lessonsmp3” in the category delete page we should remove the files related to this category after removing the selected category row from the database
here is the code example
If e.AffectedRows >0 Then
Dim categoryid As String = categoryddl.SelectedValue.ToString()
Dim file() As FileInfo
Dim i As Integer
Dim searchfor As String
Dim directory As New DirectoryInfo(Server.MapPath(“~/lessonsmp3/”))
searchfor= categoryid &“*”&“.mp3″
file = directory.GetFiles(searchfor)
If file.Length >0 Then
For i = 0 To file.Length –1
1-First we create a variable to carry the id of the category we want to remove “categoryid”.
2-then we instantiate an array of file info type object. This will give us the ability to loop through large number of files and delete or edit each file alone.
3-we define the “i” integer type variable to act as a loop counter.
4-then we define a string variable “searchfor” which will carry the search criteria of the mp3files we want to delete. Notice that we designed the lesson insert page to add the category id in front of the uploaded file name to ease the process of multiple file deleting all we have to do is to search for the category id of the category we need to remove in the start of any file name if we found the id of category we want to remove we delete the file else leave the file inplace.
5-then we define a “directoryinfo” type variable that point to the directory that contains the files that we want to delete from. Notice we used “server.mappath” to get the physical path of the directory from the virtual path which is”~/lessonsmp3/”.
6-then we set the search parameters as we mentioned before. the file should contains the category id in the start of it’s name and mp3 type.
7.then we load the files that present in the selected directory into the “file” array we created before.
8-then check if the array contains files by checking it’s length to be more than zero.
9-then we loop through the array checking if the mp3file have the criteria we set before if the file belong to the category we want to remove we delete the file also using”file(i).delete()” method.
10-continue looping to the last file in the array and you are finished.