正由另一进程使用,因此该进程无法访问该文件

来源:互联网 发布:mac远程控制ipad 编辑:程序博客网 时间:2024/06/04 23:12

 

  //if (File.Exists(strWorkPhotoPath))
            //{
                File.Delete(strWorkPhotoPath);
       //    }

 

 

上传图片的时候,偶尔会出现此异常。检查了所有用到文件流的地方,保证都close()了,但还是偶尔还会出现此异常

“如何解决IOException 正由另一进程使用,因此该进程无法访问该文件”   

 

发现File.Exists是这个引起的。

 

将  //if (File.Exists(strWorkPhotoPath))注释掉,则异常出现的几率减少。极少的时候会出现。

 

-----------------------------------------------------------------------------------------------------

以上操作不能解决问题。代码片段:

 

      //生成缩略图           //  System.Drawing.Image img = System.Drawing.Image.FromFile(strLivePhotoPath);            if (!File.Exists(strthumbnailPhotoPath))            {                myGetThumbnailImage(strLivePhotoPath, strthumbnailPhotoPath, 600, 450, "White");            }


在下面这个方法调用 File。Delete()的时候报错

 

  /// </summary>     /// <param name="SourceFile">文件在服务器上的物理地址</param>     /// <param name="strSavePathFile">保存在服务器上的路径</param>     /// <param name="ThumbWidth">宽度</param>     /// <param name="ThumbHeight">高度</param>     /// <param name="BgColor">背景</param>     public static void myGetThumbnailImage(string SourceFile, string strSavePathFile, int ThumbWidth, int ThumbHeight, string BgColor)    {        System.Drawing.Image oImg = System.Drawing.Image.FromFile(SourceFile);        //小图         int intwidth, intheight;        if (oImg.Width > oImg.Height)        {            if (oImg.Width > ThumbWidth)            {                intwidth = ThumbWidth;                intheight = (oImg.Height * ThumbWidth) / oImg.Width;            }            else            {                intwidth = oImg.Width;                intheight = oImg.Height;            }        }        else        {            if (oImg.Height > ThumbHeight)            {                intwidth = (oImg.Width * ThumbHeight) / oImg.Height; intheight = ThumbHeight;            }            else            {                intwidth = oImg.Width; intheight = oImg.Height;            }        }        //构造一个指定宽高的Bitmap         Bitmap bitmay = new Bitmap(intwidth, intheight);        Graphics g = Graphics.FromImage(bitmay);        Color myColor;        if (BgColor == null) myColor = Color.FromName("white");        else            myColor = Color.FromName(BgColor);        //用指定的颜色填充Bitmap         g.Clear(myColor);        g.InterpolationMode = InterpolationMode.HighQualityBicubic;        //开始画图         g.DrawImage(oImg, new Rectangle(0, 0, intwidth, intheight), new Rectangle(0, 0, oImg.Width, oImg.Height), GraphicsUnit.Pixel);        bitmay.Save(strSavePathFile, System.Drawing.Imaging.ImageFormat.Jpeg);        g.Dispose();        bitmay.Dispose();        oImg.Dispose();        //删除源图         try        {            File.Delete(SourceFile);        }        catch { }    }



 将 //生成缩略图  处去掉 或者 改为

 

  //生成缩略图           //  System.Drawing.Image img = System.Drawing.Image.FromFile(strLivePhotoPath);            if (!File.Exists(strthumbnailPhotoPath))            {//将图片释放掉 img.Dispose();                myGetThumbnailImage(strLivePhotoPath, strthumbnailPhotoPath, 600, 450, "White");            }


这样就不会报错了 

  //删除源图         try        {            File.Delete(SourceFile);        }        catch { }

原创粉丝点击