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.

Links…
[...]Sites of interest we have a link to[...]……
Websites worth visiting……
[...]here are some links to sites that we link to because we think they are worth visiting[...]…
ccleaner download…
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose ccleaner download [...]……
Online Article……
[...]The information mentioned in the article are some of the best available [...]……
ares free download…
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose ares free download[...]……
toronto escorts…
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose toronto escorts [...]……
Superb website…
[...]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[...]……
Gems form the internet…
[...]very few websites that happen to be detailed below,from our point of view are undoubtedly well worth checking out[...]……
………..
My spouse and i suppose in the meantime i am going to be happy with book-marking and adding your Feed in order to my Yahoo and google account. I enjoy new updates and can share this kind of website with my own Myspace party….
No Deposit Poker…
I’m now not sure where you’re getting your info,however great topic. I must spend a while studying much more or figuring out more. Thank you for magnificent info I was on the lookout for this information for my mission….
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[...]……
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[...]……
download bearshare…
[...]usually posts some very interesting stuff like this. If you’re new to this site download bearshare[...]……
Good job…
[...]very interesting subject ,outstanding post[...]…
nero free…
[...]just beneath,are numerous totally not related sites to ours,however,they are surely worth going over nero free[...]……
adobe photoshop cs5…
[...]always a big fan of linking to bloggers that I love but don’t get a lot of link love from adobe photoshop cs5[...]……
Visitor recommendations…
[...]one of our visitors recently recommended the following website[...]……
torrent pirate bay…
[...]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 torrent pirate bay[...]……
Reall nice blog…
[...]just below,are some totally unrelated sites to ours,however,they are definitely worth checking out[...]…
[...]The information mentioned in the article are some of the best available [...]……
[...]below you’ll find the link to some sites that we think you should visit[...]……
Links…
[...]Sites of interest we have a link to[...]……
Sites we Like……
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
………..
My partner and i suppose for now i’ll settle for book-marking and introducing your own Rss to be able to my Google account. I enjoy fresh changes and definately will share this kind of blog using our Facebook group….
Great information…
This is often first-class. An checked out every group feedback and we are thunderstruck. We’re curious about this kind of situations. First appreciate your personal opinion,and significance doing with this. Please keep modifying. They’re unbelievabl…
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[...]……
Awesome website…
[...]the time to read or visit the content or sites we have linked to below the[...]……
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[...]……
btjunkie download…
[...]below you’ll find the link to some sites that we think you should visit btjunkie download[...]……
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
You should check this out…
[...] Wonderful story,reckoned we could combine a few unrelated data,nevertheless really worth taking a look,whoa did one learn about Mid East has got more problerms as well [...]……
frostwire free download…
[...]The information mentioned in the article are some of the best available frostwire free download[...]……
You should check this out…
[...] Wonderful story,reckoned we could combine a few unrelated data,nevertheless really worth taking a look,whoa did one learn about Mid East has got more problerms as well [...]……
Good job…
[...]very interesting topic,outstanding post[...]…
You should check this out…
[...] Wonderful story,reckoned we could combine a few unrelated data,nevertheless really worth taking a look,whoa did one learn about Mid East has got more problerms as well [...]……
Wikia…
Wika linked to this site…
Here is something I found out today:…
I recommend you check out this quality article…
Sites we Like……
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……
Links…
[...]Sites of interest we have a link to[...]……
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[...]……
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[...]……
Visitor recommendations…
[...]one of our visitors recently recommended the following website[...]……
Links…
[...]Sites of interest we have a link to[...]……
You should check this out…
[...] Wonderful story,reckoned we could combine a few unrelated data,nevertheless really worth taking a look,whoa did one learn about Mid East has got more problerms as well [...]……
Websites you should visit…
[...]below you’ll find the link to some sites that we think you should visit[...]……
Cool sites……
[...]we came across a cool site that you might enjoy. Take a look if you want[...]………
Related……
[...]just beneath,are numerous totally not related sites to ours,however,they are surely worth going over[...]……
[...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate,just click the links over[...]……
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
You should check this out…
[...] Wonderful story,reckoned we could combine a few unrelated data,nevertheless really worth taking a look,whoa did one learn about Mid East has got more problerms as well [...]……