Click here to Skip to main content
15,913,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had uploaded a file in the database and that file is also contains by a folder of the website page in asp.net.
Now i want to delete the file from the database and the folder also. I have successfully deleted the
file from the database but file is not deleting from the folder of the website.
so tell me the proper answer.
The whole detail is given below:-

Firstly I had uploaded a file(whether it is image,word file etc.)Now u can
understand that file will also go to the folder .It mean to say that file will go the database(sql server) and also go to the folder of the website.

Now If i want to delete that file then it is deleting from the databse(sql server)
but not to deleting from the folder of the website(asp.net).Folder name like (image,upload etc.)

Here is my c# code for delete a file from the database and the folder also.The folder name is "Uploads" which you can see in the code and the databse table name is "emp" so now you can understand that file is deleting from the database table "emp" but not deleting from the folder "Uploads".

Code is given below:-

C#
protected void btndelete_Click(object sender, EventArgs e)
    {
 string connectionString = WebConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
        
          string filename = FileUpload1.FileName;
          FileUpload1.PostedFile.Equals(Server.MapPath("~\\Uploads\\" + filename));
          string path = "~\\Uploads\\" + filename;

       SqlConnection cnn = new SqlConnection(connectionString);
        try
        {
            cnn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "delete from emp ";
            
            cmd.CommandType = CommandType.Text;
            cmd.Connection = cnn;
            cmd.ExecuteNonQuery();

          if (File.Exists(path))
            {
                File.Delete(path);
                 
            }

           
            cnn.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {
           lblinfo2.Text = "";
           lblinfo2.Text= " Deleted Successfully ";
        }

    }
Posted
Updated 13-Sep-11 3:12am
v2

Please visit following link

ASP.NET File Upload with Progress Bar[^]
 
Share this answer
 
 
Share this answer
 
in finally block write this
C#
if (File.Exists(path))
            {
                File.Delete(path);

            }
 
Share this answer
 
use file info

using System.io

 File.Delete(Server.MapPath("../Nurturing/" + fnevents));
            FileInfo fInfoEvent;
            fInfoEvent = new FileInfo(fnevents);
            fInfoEvent.Delete();



here fnevents is the name of the file that u are deleting. Nurturing is the name of the folder.

It will do the work for you
 
Share this answer
 
Refer this
http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx[^]
This works fine for me
C#
string Path = Server.MapPath("~/JPEG/Document/" + Session["ID"].ToString())+"Tble" + test+ ".jpg";
if (System.IO.File.Exists(Path))
{

System.IO.File.Delete(Path);

}
 
Share this answer
 
v2
Make sure the ASP user has permissions to this folder. By default this user is not given access to much of the hard drive.

using System.IO;

First Way
C#
protected void Button1_Click(object sender, System.EventArgs e) {
       string filePath = Request.PhysicalApplicationPath + "Elephant.jpg";

       try
       {
           File.Delete(filePath);
           Label1.Text = "File deleted";

       }
       catch(Exception ex)
       {
           Label1.Text = "an error occured<br />";
           Label1.Text = ex.Message.ToString();
       }
   }


Second Way

C#
try {
FileInfo TheFile = new FileInfo(MapPath(".") + "\\" + txtFile.Text);
if (TheFile.Exists) {
File.Delete(MapPath(".") + "\\" + txtFile.Text);
}
else {
throw new FileNotFoundException();
}
}
 
Share this answer
 
Comments
Richard C Bishop 17-Apr-13 12:57pm    
This thread is two years old. Be sure to check the dates as this can be construed as abuse.
[no name] 17-Apr-13 13:04pm    
i m going with somw intrest so i didnt listen date...,
Richard C Bishop 17-Apr-13 13:35pm    
No worries, just a friendly reminder for futuer reference.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900