How to delete a specific file from folder using asp.net

后端 未结 5 615
感情败类
感情败类 2021-02-01 21:12

here\'s the deal I got a datagridviewer which is called gridview1 and a fileupload1 when i upload a file it updates the gridview1 and table in database with the file name and pa

5条回答
  •  余生分开走
    2021-02-01 21:24

    This is how I delete files

    if ((System.IO.File.Exists(fileName)))
                    {
                        System.IO.File.Delete(fileName);
    }
    

    Also make sure that the file name you are passing in your delete, is the accurate path

    EDIT

    You could use the following event instead as well or just use the code in this snippet and use in your method

    void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
      {
    
        // Get the currently selected row using the SelectedRow property.
        GridViewRow row = CustomersGridView.SelectedRow;
    
        //Debug this line and see what value is returned if it contains the full path.
        //If it does not contain the full path then add the path to the string.
        string fileName = row.Cells[0].Text 
    
        if(fileName != null || fileName != string.empty)
        {
           if((System.IO.File.Exists(fileName))
               System.IO.File.Delete(fileName);
    
         }
      }
    

提交回复
热议问题