Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

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 path and stores the said file in folder "Mag"... but now what i want to do is the reverse i got how to use the gridview to delete the table entry but deleting the file from folder "Mag" is not working have used the following code in C# or codebehind

protected void GridView1_Del(object sender, EventArgs e)  
{
    string DeleteThis = GridView1.SelectedRow.Cells[0].Text;
    string[] Files = Directory.GetFiles(@"i:/Website/WebSite3/Mag/");

    foreach (string file in Files)
    {
        if (file.ToUpper().Contains(DeleteThis.ToUpper()))
        {
            File.Delete(file);
        }
    }
}

it gives me error

"Object reference not set to an instance of an object."

pls tell me what im doing wrong am new and don't have to in depth understanding of the platform so any and all help will be appreciated thanks in advance Mark

Here is the answer i found Thanks Tammy and everyone else for all the answers

Ok here the deal target function delete file details from gridview and database table and file from project folder where the file is stored

in script section of gridview you would want to include

OnRowDeleting="FuntionName"

Not

OnSelectedIndexChanged = "FuntionName"

or

OnRowDeleted="FuntionName"

then in C# code(codebehind)

protected void FuntionName(object sender, GridViewDeleteEventArgs e)
    {
// storing value from cell
        TableCell cell = GridView1.Rows[e.RowIndex].Cells[0];

// full path required
        string fileName = ("i:/Website/WebSite3/Mag/" + cell.Text); 

    if(fileName != null || fileName != string.Empty)
    {
       if((System.IO.File.Exists(fileName))) 
       {
           System.IO.File.Delete(fileName);
       }

     }
  }

And just for added reference for those who want to learn

OnRowDeleting="FuntionName" is for just before deleting a row you can cancel deleting or run functions on the data like i did

OnRowDeleted="FuntionName" it directly deletes

like image 621
Mark Avatar asked Jan 10 '13 05:01

Mark


People also ask

How can we delete a file in C#?

The remove function in C/C++ can be used to delete a file. The function returns 0 if files is deleted successfully, other returns a non-zero value. Using remove() function in C, we can write a program which can destroy itself after it is compiled and executed.

How do I delete a file in VB NET?

To delete a text file and ask the user to confirm that the file should be deleted. Use the DeleteFile method to delete the file, setting showUI to AllDialogs . The following code demonstrates how to delete the file named test. txt and allow the user to confirm that the file should be deleted.


2 Answers

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

     }
  }
like image 111
tam tam Avatar answered Sep 21 '22 05:09

tam tam


Check the GridView1.SelectedRow is not null:

if (GridView1.SelectedRow == null) return;
string DeleteThis = GridView1.SelectedRow.Cells[0].Text;
like image 20
Jeremy Thompson Avatar answered Sep 20 '22 05:09

Jeremy Thompson