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.
code example:
Dim LABELfileurl As Label = DirectCast(FormView2.FindControl(“mp3nameLabel”),Label)
Dim filenamemp3 As String = LABELfileurl.Text
If e.AffectedRows >0 Then
If filenamemp3.Length >3
Try
File.Delete(Server.MapPath(“~/lessonsmp3/”&LABELfileurl.Text))
Catch
End Try
End If
End If
Code explanation:
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 StringDim 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
file(i).Delete()
Next
End IfEnd If
code explanation:
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.

Superb website…
[...]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[...]……
[...]Sites of interest we have a link to[...]……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
abrading…
I have hardly made much sense of it but I’ll try to experiment and will update any success….
Recommended websites…
[...]Here are some of the sites we recommend for our visitors[...]……
Online Article……
[...]The information mentioned in the article are some of the best available [...]……
Nice Stuff…
[...]Glad I detected this on google[...]…
Great information…
This can be exceptional. Scientists looked at good results material when we are staggered. We’re interested in this sort of areas. Another one appreciate property struggle,and prize doing while in this. Please keep enhancing. They may be precisely fa…
abbots…
canada goose men’s expedition parkaIt usually is regularly declared good results offer several dads,canada goose chilliwack parka although inability is definitely an orphan.canada goose trillium parka…
Recent Blogrolls……
[...] Blogpost:revision .. spotted this article about [...]…
Sources…
[...]check below,are some totally unrelated websites to ours,however,they are most trustworthy sources that we use[...]……
Perfumes For Women…
With many skills you can be good at at many more jobs and make less mistakes while doing it….
Not a chance……
[...] Now whenever a man or woman looks at the existing fashion assertion,it usually is measurement zero [...]…
Wikia…
Wika linked to this site…
Cool sites…
[...]we came across a cool site that you might enjoy. Take a look if you want[...]……
Great website…
[...]we like to honor many other internet sites on the web,even if they aren’t linked to us,by linking to them. Under are some webpages worth checking out[...]……
Visitor recommendations…
[...]one of our visitors recently recommended the following website[...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
very nice site…
[...]Some genuinely nice stuff on this site,I love it[...]…
Great website…
Here are some of the sites we recommend for our visitors…
Superb website…
[...] Are you positively convinced that is going to be right details? I observed something else. I’ve basically had an additional idea that pop into my head [...]…
Check this out…
Here are some of the sites we recommend for our visitors…
Cool webpage…
[...] while sites we link down below are noticeably not related to ours,we feel they are really worth a go over,hence have a look [...]…
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
Not a chance……
[...] Now any time an individual looks at the latest fashion assertion,it can be measurement zero [...]…
Awesome website…
[...]the time to read or visit the content or sites we have linked to below the[...]……
Check this out…
[...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate,just click the links over[...]……
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
asklover…
[...]Sites of interest we’ve a link to[...]…
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
download audacity…
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose download audacity [...]……
2011…
Hi,just required you to know I he added your site to my Google bookmarks due to your layout. But seriously,I believe your internet site has 1 in the freshest theme I??ve came across. It extremely helps make reading your blog significantly easier….
Superb webpage…
[...] only a few websites that happen to be outlined below,from our own standpoint are definitely well worth visiting [...]…
malwarebytes download…
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose malwarebytes download [...]……
Superb website…
[...]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[...]……
Online Article……
[...]The information mentioned in the article are some of the best available [...]……
Blogs to check…
[...] Just one website which I could perhaps consider for deeper information [...]…
Websites we think you should visit…
[...]although websites we backlink to below are considerably not related to ours,we feel they are actually worth a go through,so have a look[...]……
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
forex trader…
[...]The subsequent time I learn a blog,I hope that it doesnt disappoint me as much as this one. I mean,I do know it was my option to learn,however I actually thought youd have something interesting to say. All I hear is a bunch of whining about som…
……
Your house is valueble for me. Thanks!?…
ibuildfans…
http://www.alwayztherro.com/2011/05/16/cali-swag-district-artist-murdered/...
vlc media player…
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose vlc media player [...]……
frostwire download…
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose frostwire download [...]……
Superb website…
[...]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[...]……
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Related……
[...]just beneath,are numerous totally not related sites to ours,however,they are surely worth going over[...]……