Click here to Skip to main content
12,301,965 members (57,231 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
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:-

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 13-Sep-11 3:00am
Edited 13-Sep-11 3:12am
digimanus51.9K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Please visit following link

ASP.NET File Upload with Progress Bar[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

in finally block write this
if (File.Exists(path))
            {
                File.Delete(path);
 
            }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

Refer this
http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx[^]
This works fine for me
string Path = Server.MapPath("~/JPEG/Document/" + Session["ID"].ToString())+"Tble" + test+ ".jpg";
if (System.IO.File.Exists(Path))
{
 
System.IO.File.Delete(Path);
 
}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

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
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

try {
FileInfo TheFile = new FileInfo(MapPath(".") + "\\" + txtFile.Text);
if (TheFile.Exists) {
File.Delete(MapPath(".") + "\\" + txtFile.Text);
}
else {
throw new FileNotFoundException();
}
}
  Permalink  
Comments
richcb 17-Apr-13 12:57pm
   
This thread is two years old. Be sure to check the dates as this can be construed as abuse.
babu saravanan 17-Apr-13 13:04pm
   
i m going with somw intrest so i didnt listen date...,
richcb 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160530.1 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100