Delete picture from Image folder

Last post 07-07-2009 5:57 AM by venkatu2005. 12 replies.

Sort Posts:

  • Delete picture from Image folder

    07-07-2009, 3:40 AM
    • Member
      21 point Member
    • RituTokhi
    • Member since 06-11-2009, 9:57 AM
    • Chandigarh
    • Posts 17

     i am using gridview and in gridview i have a checkbox column and i using delete button to delete the Checked  record. i am uploading images through asp.net fileUpload Server Control. and in a DataBase i am just Storing the Image Name with its Extension for ex  Image1.jpg and same name i am storing in Images folder in my Application root path.
    now problem is when i delete any record then that images should also delete from the Images folder from root path     how to do that..?.

    how to find perticular id and that file name from d'base and match with image name in Image Folder.
    can any one give me a solution...?

  • Re: Delete picture from Image folder

    07-07-2009, 4:22 AM
    • Participant
      1,470 point Participant
    • Heloril
    • Member since 10-30-2007, 1:38 PM
    • Belgium
    • Posts 255

     Hi,

    Try this:

    System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath("~/image1.jpg"));
            if (fi.Exists)
                fi.Delete();


     

    Mark as Answer if this reply helps you

    My blog: http://www.notesfor.net/
  • Re: Delete picture from Image folder

    07-07-2009, 4:26 AM
    • All-Star
      36,316 point All-Star
    • rtpHarry
    • Member since 10-01-2006, 12:51 PM
    • Lincoln, England
    • Posts 5,847

    Hey,

    You can delete it in the OnRowDeleting event of the gridview.


    If you store the image path in the gridview (either show it or put it in DataKeyNames) then you will be able to get the path during this event without having to do a select.


    You can use File.Delete() to remove the image from the filesystem.



    If you have built a datalayer then you can do this in your delete method so that it is always deleted when you call it and you dont have to rewrite it for each page.

  • Re: Delete picture from Image folder

    07-07-2009, 4:29 AM
    • All-Star
      36,316 point All-Star
    • rtpHarry
    • Member since 10-01-2006, 12:51 PM
    • Lincoln, England
    • Posts 5,847

    heloril:

     Hi,

    Try this:

    1. System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath("~/image1.jpg"));  
    2.         if (fi.Exists)  
    3.             fi.Delete();  


     


    Fyi - You dont need to check if the file exists as an exception is not thrown if you call file.delete on a file that doesnt exist. (see msdn docs link above)

  • Re: Delete picture from Image folder

    07-07-2009, 4:32 AM
    • Member
      561 point Member
    • harieis
    • Member since 04-23-2009, 12:11 PM
    • Trivandrum
    • Posts 150

    using System.IO;



    File.Delete(Server.MapPath("FolderName/YourFileNameWithExtension"));


    This code will help You!

    Please, Mark as Answer if this reply helped you
    ==============================================

    Harikrishnan.S
    Trivandrum,Technopark,
    Kerala,
    India
    harikrishnans82@gmail.com
    ==================================================
  • Re: Delete picture from Image folder

    07-07-2009, 4:34 AM
    • Participant
      1,430 point Participant
    • prajeeshkkindia
    • Member since 12-15-2007, 6:58 AM
    • India
    • Posts 259

    protected void btnDeletet_Click(object sender, EventArgs e)

    {

    try {

    FileInfo TheFile = new FileInfo(MapPath(".") + "\\" + txtFile.Text);

    if (TheFile.Exists) {

    File.Delete(MapPath(".") + "\\" + txtFile.Text);

    }

    else {

    throw new FileNotFoundException();

    }

    }



    catch (FileNotFoundException ex) {

    lblStatus.Text += ex.Message;

    }

    catch (Exception ex) {

    lblStatus.Text += ex.Message;

    }



    }


    dont forgot to import
    using System.IO;

    in the begining of the class


    If this post is helpful, please mark as an answer.

    Regards,
    Prajeesh.
    Software Engineer
    Speridian Technologies




    Filed under:
  • Re: Delete picture from Image folder

    07-07-2009, 4:58 AM

    Hi,

    Use   File.Delete

                eg: Your folder name is images then

      File.Delete(Server.MapPath("images") & "/" & "winter.jpg")

    Thanks :)

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

    Srinivas Kotra.


  • Re: Delete picture from Image folder

    07-07-2009, 5:10 AM
    • Member
      21 point Member
    • RituTokhi
    • Member since 06-11-2009, 9:57 AM
    • Chandigarh
    • Posts 17

    Here issue is how to delete image from folder when i delete the record which belongs to that image. plz help me out

  • Re: Delete picture from Image folder

    07-07-2009, 5:15 AM
    • All-Star
      21,070 point All-Star
    • venkatu2005
    • Member since 07-01-2008, 10:48 AM
    • Posts 4,586

    RituTokhi:

    Here issue is how to delete image from folder when i delete the record which belongs to that image. plz help me out

    You have to write the code on where you are going to write the delete query there get the image name (ex: sample.jpg) while you click the Delete button then pass the image before you delete query to delete the image from the folde by writing the code

    System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath("~/sample.jpg"));
    if (fi.Exists)
    fi.Delete();


    I have Changed My Blog from (http://venkat-dotnetsamples.blogspot.com) to (http://venkat-dotnetsnippets.blogspot.com)

    Regards,
    Venkatesan.M

    Please Mark as Answered If its helpful and Un-Mark as Answered if it not help u.
  • Re: Delete picture from Image folder

    07-07-2009, 5:30 AM
    • Member
      21 point Member
    • RituTokhi
    • Member since 06-11-2009, 9:57 AM
    • Chandigarh
    • Posts 17

    how to get image name? This is my delete code.im using stored procedure.


    protected void btnDelete_Click(object sender, EventArgs e)

    {

         string CheckValues = string.Empty;

         for (Int32 i = 0; i < gvList.Rows.Count; i++)

     {

    CheckBox chk = (CheckBox)gvList.Rows[i].FindControl("CheckBoxbtn");

     if (chk.Checked == true)

    {

    CheckValues = CheckValues + "'"+((Label)gvList.Rows[i].FindControl("lblId")).Text+"'" + ",";

    }

     }

     if (CheckValues != "")

    {

    CheckValues = CheckValues.Substring(0, CheckValues.Length - 1);

    }

    objNewes.ID = CheckValues; 

    objNewes.DeleteAnnouncement();

     }

     

     

  • Re: Delete picture from Image folder

    07-07-2009, 5:37 AM
    • Member
      561 point Member
    • harieis
    • Member since 04-23-2009, 12:11 PM
    • Trivandrum
    • Posts 150

    In ur table, there is filed to the image save.Rite?

    I think the image path/ImageName will be saved into the db..

    before you delete the record(1 row),You can get the imageName from the db and passs the

    imageName to this line.ok? got?


    File.Delete(Server.MapPath("FolderName/'+YourImageName+'"));


    Got????????


    Or plese paste ur code fully.................................


    Please, Mark as Answer if this reply helped you
    ==============================================

    Harikrishnan.S
    Trivandrum,Technopark,
    Kerala,
    India
    harikrishnans82@gmail.com
    ==================================================
  • Re: Delete picture from Image folder

    07-07-2009, 5:56 AM
    • Member
      561 point Member
    • harieis
    • Member since 04-23-2009, 12:11 PM
    • Trivandrum
    • Posts 150


    objNewes.ID = CheckValues; 


    After this line You will get the id like this '1,2,3,4,5'

    and before we delete this  you have to take the image names using this ids(use your logic).

    then pass the old code for delete..((((((((File.Delete...etc............. got?

    then write......................

    objNewes.DeleteAnnouncement();

    Please, Mark as Answer if this reply helped you
    ==============================================

    Harikrishnan.S
    Trivandrum,Technopark,
    Kerala,
    India
    harikrishnans82@gmail.com
    ==================================================
  • Re: Delete picture from Image folder

    07-07-2009, 5:57 AM
    Answer
    • All-Star
      21,070 point All-Star
    • venkatu2005
    • Member since 07-01-2008, 10:48 AM
    • Posts 4,586

    Here , what you do , ie in your Gridview  Place Label or Hiddenfield so bind the image name to Label like this

    <asp:label id ="Label1" runat="server" Text='<%# Eval("imagename") %>' />


    Then on you button delete get the image name

    protected void btnDelete_Click(object sender, EventArgs e)
    
    {
    
         string CheckValues = string.Empty;
    
         for (Int32 i = 0; i < gvList.Rows.Count; i++)
    
     {
    
    CheckBox chk = (CheckBox)gvList.Rows[i].FindControl("CheckBoxbtn");
    
     if (chk.Checked == true)
    
    {
    
    CheckValues = CheckValues + "'"+((Label)gvList.Rows[i].FindControl("lblId")).Text+"'" + ",";
    
    Label Lblimagename= (Label)gvList.Rows[i].FindControl("Label1")).Text;
    
    // so you get the  image name by usgin Lblimagename.Text so using this delete 
    
    // the image , the code that i gave already check it.
    
    }
    
     }
    
     if (CheckValues != "")
    
    {
    
    CheckValues = CheckValues.Substring(0, CheckValues.Length - 1);
    
    }
    
    objNewes.ID = CheckValues; 
    
    objNewes.DeleteAnnouncement();
    
     } 


    I have Changed My Blog from (http://venkat-dotnetsamples.blogspot.com) to (http://venkat-dotnetsnippets.blogspot.com)

    Regards,
    Venkatesan.M

    Please Mark as Answered If its helpful and Un-Mark as Answered if it not help u.
Page 1 of 1 (13 items)