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();
}
}
}
私は、この行動に関する他のアイデアを喜んで受け入れます。