PaisaFever Shakshi
PaisaFever Shakshi

Reputation: 31

How to delete file from directory using vb.net?

I have a asp.net website folder namely Website3 inside website3 folder there is a upload folder. I want to delete files inside upload folder on button click event.

MY directory status :

Website3 > upload

I'm using the following command but it doesn't delete the file from upload directory

Protected Sub ListView1_ItemDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewDeletedEventArgs) Handles ListView1.ItemDeleted
        Dim lab As Label = CType(ListView1.Items(ListView1.SelectedValue).FindControl("photoLabel"), Label)
        System.IO.File.Delete(System.IO.Path.GetDirectoryName("/upload/") & lab.Text)
    End Sub

Upvotes: 3

Views: 20030

Answers (4)

Rajeshkumar Kandhasamy
Rajeshkumar Kandhasamy

Reputation: 6461

Just try this:

Public Function DelAllUploadedFiles()    
For Each Uploadedfiles As var In System.IO.Directory.GetFiles(Server.MapPath("~/upload/"))        
System.IO.File.Delete(Uploadedfiles )   
Next
End Function

Upvotes: 1

Jan Sterenborg
Jan Sterenborg

Reputation: 1

First you have to set the folder settings on the server who can enter this folder and has delete authority Login to your server account and give delete permission to the folder to everyone

Upvotes: 0

Morten Anderson
Morten Anderson

Reputation: 2321

You should be using Server.MapPath

File.Delete((MapPath(".") + ("\\" + lab.Text)))

Upvotes: 4

Gregory Nozik
Gregory Nozik

Reputation: 3374

Try to put try and catch and see if you have Exception . In general in order to delete in IIS process pool ,should be defined under strong user .

Upvotes: 0

Related Questions