程序上传到服务器,删除图片,
报sys.webforms.pagerequestmanagerservererrorexception:文件“E:***”正由另一进程使用,因此该进程无法访问该文件在本机运行删除是没有问题的,但上传服务器后出现这个问题。部分代码如下: /// <summary>
/// 删除相片按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnDele_Click(object sender, EventArgs e)
{
int photoId = Convert.ToInt32(ViewState["PID"]);
MyPhotos photo = MyPhotosManager.GetMyPhotosByMyPhotosId(photoId);
MyAlbum album = MyAlbumManager.GetMyAlbumByAlbumId(photo.AlbumId);
if (album.HomePhotos == photoId) //如果要删除的相片为相册封面
{
album.HomePhotos = 0; //设置封面字段为0
MyAlbumManager.UpdateAlbum(album);
}
else
{
DeleFile(photo.SavePath); //删除在硬盘上的图片
}
List<PhotoComment> list = PhotoCommentManager.GetAllCommentByPhotoId(photoId);
foreach (PhotoComment c in list)
{
List<ToReply> toList = ToReplyManager.GetAllReplyByCommentId(c.PhotoCommentId);
foreach (ToReply t in toList)
{
ToReplyManager.DeleToReplyByToReplyId(t.ToReplyId); //删除所有回复
}
}
PhotoCommentManager.DeleCommentByPhotoId(photoId); //删除图片的所有评论 int i = MyPhotosManager.DelePhotoById(photoId); //从数据库中删除图片
MyAlbum newAlbum = MyAlbumManager.GetMyAlbumByAlbumId(photo.AlbumId);
int count = newAlbum.PictureNum;
newAlbum.PictureNum = count - 1;
int j = MyAlbumManager.UpdateAlbum(newAlbum); //更新相册照片数 List<MyPhotos> alist = MyPhotosManager.GetAllByAlbumId(newAlbum.AlbumId); if (alist.Count > 0)
{
PhotoRreOrNext(0); //删除照片后,显示下一张照片
}
else
{
Response.Redirect("photo.aspx"); //如果这个相册只有一张照片,删除就跳入相册首页面
}
} /// <summary>
/// 删除文件
/// </summary>
/// <param name="filePath"></param>
public void DeleFile(string filePath)
{
try
{
string path = Server.MapPath(filePath);
System.IO.FileInfo DeleFile = new System.IO.FileInfo(path);
if (DeleFile.Exists)
{
DeleFile.Delete();
}
}
catch (Exception ex)
{
throw ex;
}
}
报sys.webforms.pagerequestmanagerservererrorexception:文件“E:***”正由另一进程使用,因此该进程无法访问该文件在本机运行删除是没有问题的,但上传服务器后出现这个问题。部分代码如下: /// <summary>
/// 删除相片按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnDele_Click(object sender, EventArgs e)
{
int photoId = Convert.ToInt32(ViewState["PID"]);
MyPhotos photo = MyPhotosManager.GetMyPhotosByMyPhotosId(photoId);
MyAlbum album = MyAlbumManager.GetMyAlbumByAlbumId(photo.AlbumId);
if (album.HomePhotos == photoId) //如果要删除的相片为相册封面
{
album.HomePhotos = 0; //设置封面字段为0
MyAlbumManager.UpdateAlbum(album);
}
else
{
DeleFile(photo.SavePath); //删除在硬盘上的图片
}
List<PhotoComment> list = PhotoCommentManager.GetAllCommentByPhotoId(photoId);
foreach (PhotoComment c in list)
{
List<ToReply> toList = ToReplyManager.GetAllReplyByCommentId(c.PhotoCommentId);
foreach (ToReply t in toList)
{
ToReplyManager.DeleToReplyByToReplyId(t.ToReplyId); //删除所有回复
}
}
PhotoCommentManager.DeleCommentByPhotoId(photoId); //删除图片的所有评论 int i = MyPhotosManager.DelePhotoById(photoId); //从数据库中删除图片
MyAlbum newAlbum = MyAlbumManager.GetMyAlbumByAlbumId(photo.AlbumId);
int count = newAlbum.PictureNum;
newAlbum.PictureNum = count - 1;
int j = MyAlbumManager.UpdateAlbum(newAlbum); //更新相册照片数 List<MyPhotos> alist = MyPhotosManager.GetAllByAlbumId(newAlbum.AlbumId); if (alist.Count > 0)
{
PhotoRreOrNext(0); //删除照片后,显示下一张照片
}
else
{
Response.Redirect("photo.aspx"); //如果这个相册只有一张照片,删除就跳入相册首页面
}
} /// <summary>
/// 删除文件
/// </summary>
/// <param name="filePath"></param>
public void DeleFile(string filePath)
{
try
{
string path = Server.MapPath(filePath);
System.IO.FileInfo DeleFile = new System.IO.FileInfo(path);
if (DeleFile.Exists)
{
DeleFile.Delete();
}
}
catch (Exception ex)
{
throw ex;
}
}
我也感觉是没释放。
加载图片的时候看看有没有在改变像素的时候
System.Drawing.Image没有Dispose();
/// 加载时显示页面信息
/// </summary>
/// <param name="photoId"></param>
protected void Display(int photoId)
{
MyPhotos photo = MyPhotosManager.GetMyPhotosByMyPhotosId(photoId);
string path = Server.MapPath(photo.SavePath); //得到图片的物理路径
System.Drawing.Image image = System.Drawing.Image.FromFile(path); //生成原图
double height = Convert.ToDouble(image.Height); //得到原图的高度
double width = Convert.ToDouble(image.Width); //得到原图的宽度
MyAlbum album = MyAlbumManager.GetMyAlbumByAlbumId(photo.AlbumId);
if (width <= 550 && height <= 550) //如果小于最大高、宽,原始大小显示
{
imgHome.ImageUrl = photo.SavePath; }
else if (width <= 550 && height > 550)
{
double flat = width / height;
imgHome.Width = (int)(550 * flat);
imgHome.Height = 550;
imgHome.ImageUrl = photo.SavePath;
}
else if (width > 550 && height <= 550)
{
double flat = height / width;
imgHome.Width = 550;
imgHome.Height =(int)(550 * flat);
imgHome.ImageUrl = photo.SavePath;
}
else
{
if (width > height)
{
double flat = height / width;
imgHome.Width = 550;
imgHome.Height = (int)(550 * flat);
imgHome.ImageUrl = photo.SavePath;
}
else
{
double flat = width / height;
imgHome.Width = (int)(550 * flat);
imgHome.Height = 550;
imgHome.ImageUrl = photo.SavePath;
}
}
lblPostion.Text = album.AlbumName;
lblPostion2.Text = photo.PhotosName;
txtName.Text = photo.PhotosName;
lblCreateDate.Text = photo.CreateTime.ToShortDateString();
txtDesc.Text = photo.Description; image.Dispose();
}
建议使用using括起来,保证image.Dispose();执行到。
或者用个Try Cacth,把image.Dispose(); 写到finally里面试试。
catch 释放资源 重写 dispose方法
引用 12 楼 happy664618843 的回复:
try
catch 释放资源 重写 dispose方法高端啊,这个困扰我多个月的问题您这一句代码就解决了!大师!!!!!!!