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

本文探讨了一个在图片上传过程中遇到的IOException异常问题,通过分析代码并尝试多种解决方案,最终找到问题根源并提供了解决方法。重点在于解决异常正由另一进程使用,因此该进程无法访问该文件的出现,以及如何避免在处理文件流时出现此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

  //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 { }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值