如何使用asp.net从文件夹中删除特定文件 [英] How to delete a specific file from folder using asp.net

查看:87
本文介绍了如何使用asp.net从文件夹中删除特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一笔交易,当我上传文件时,我得到了一个名为gridview1的datagridviewer和一个fileupload1,它使用文件名和路径更新了数据库中的gridview1和表,并将所述文件存储在文件夹"Mag"中...但是现在我想要做的是相反的操作,我得到了如何使用gridview删除表条目,但是从"Mag"文件夹删除文件不起作用的方法,已在C#或codebehind中使用了以下代码

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

它给我错误

对象引用未设置为对象的实例."

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

pls告诉我,我做错了什么是新的,不需要深入了解平台,因此将不胜感激.提前致谢标记

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

在这里,交易目标函数从gridview和数据库表中删除文件详细信息,并从存储文件的项目文件夹中删除文件

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

要包含在gridview的脚本部分中

in script section of gridview you would want to include

OnRowDeleting="FuntionName"

不是

OnSelectedIndexChanged = "FuntionName"

OnRowDeleted="FuntionName"

然后使用C#代码(代码隐藏)

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"用于删除行之前,您可以像我一样取消对数据的删除或运行功能

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

它直接删除的 OnRowDeleted ="FuntionName"

OnRowDeleted="FuntionName" it directly deletes

推荐答案

这是我删除文件的方式

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

编辑

您也可以使用以下事件,也可以仅使用此代码段中的代码并在您的方法中使用

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

     }
  }

这篇关于如何使用asp.net从文件夹中删除特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆