【发布时间】:2012-03-18 10:34:02
【问题描述】:
我试图从 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();
}
}
}
对于此操作,我非常愿意接受任何其他想法。
【问题讨论】:
-
在尝试解决如何删除锁定的文件之前,我会尝试知道文件被锁定的原因和位置!
-
好的,我怎么知道是什么锁定了文件?