如何从ASP.NET中的目录中删除文件? [英] How to Delete Files from Directory in ASP.NET ?
问题描述
我已将一些图片上传到fol 使用上传选项并在
gridview 上显示同一页。
我有另一个按钮'删除'。我希望在删除按钮时删除在gridview中选中的复选框。
按下删除按钮后我尝试了以下代码,但是不工作。
protected void CarImgDelBtn_Click(object sender,EventArgs e)
{
GridViewRow row = Carimgdispgrdview.SelectedRow;
string filename = row.Cells [0] .Text;
if(filename!= null || filename!= string.Empty)
{
if((System.IO.File.Exists(filename)))
System.IO.File.Delete(filename);
}
}
确保文件名包含完整路径(包括磁盘和目录)。要构建路径,可能应该使用与上载操作类似的技术(可能是
Server.MapPath )。然后将"||"替换为"&&"或试试这个:
if(!string.IsNullOrEmpty(filename))
{
string full_path =构建完整路径...
File.Delete(full_path);
}
您可能也对检测所选复选框感兴趣。这里可以询问与ASP相关的具体问题:
https://forums.asp.net 。似乎你可以为每一行使用一个循环,并且
FindControl :
https://www.asp.net/web-forms/overview/data-access/enhancing-the-gridview /添加-A-gridview的柱 - 的-复选框-CS 。
Hi,
I have uploaded some images to a folder using Upload option and displayed them in gridview on the same page.
I have another button 'Delete'. I want that checkbox selected in gridview gets deleted when I press Delete button.
I have tried the following code after pressing delete button, but is not working.
protected void CarImgDelBtn_Click(object sender, EventArgs e) { GridViewRow row = Carimgdispgrdview.SelectedRow; string filename = row.Cells[0].Text; if(filename != null || filename != string.Empty) { if ((System.IO.File.Exists(filename))) System.IO.File.Delete(filename); } }
Make sure that filename contains a full path (including disk and directories). To build the path, probably you should use a similar technique as in your Upload operations (maybe Server.MapPath). Then replace "||" with "&&" or try this:
if( ! string.IsNullOrEmpty( filename))
{
string full_path = build the full path…
File.Delete( full_path);
}You probably are also interested in detecting the selected checkboxes. Such specific questions related to ASP can be asked here: https://forums.asp.net. Seems that you can use a loop for each row, and FindControl: https://www.asp.net/web-forms/overview/data-access/enhancing-the-gridview/adding-a-gridview-column-of-checkboxes-cs.
这篇关于如何从ASP.NET中的目录中删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!