我试图从 asp.net 应用程序的目录中删除一个文件,但我总是得到一个异常,即该文件正在被另一个进程使用,这可能是由应用程序本身引起的。
这是代码
TableCell cell = (TableCell)PhotoGalleryGridView.Rows[e.RowIndex].Cells[1];
DirectoryInfo photoGalleryDirectory = new DirectoryInfo(Server.MapPath("~/GalleryImages/"));
FileInfo[] imgFiles = photoGalleryDirectory.GetFiles();
for (int i = 0; i < imgFiles.Length; i++)
{
if (imgFiles[i].Name == cell.Text)
{
imgFiles[i].Delete();
}
}
我应该如何执行此操作?谢谢
我将其实现为使用 gridview 中的 OnRowDeleting 方法的一部分,因此当我从数据库中删除照片时,它也会从站点文件夹中删除照片。
protected void PhotoGalleryGridView_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
TableCell cell = (TableCell)PhotoGalleryGridView.Rows[e.RowIndex].Cells[1];
DirectoryInfo photoGalleryDirectory = new DirectoryInfo(Server.MapPath("~/GalleryImages/"));
FileInfo[] imgFiles = photoGalleryDirectory.GetFiles();
for (int i = 0; i < imgFiles.Length; i++)
{
if (imgFiles[i].Name == cell.Text)
{
imgFiles[i].Delete();
}
}
}
对于此操作,我非常愿意接受任何其他想法。