我在做学员上传头像时,想判断上传的头像是否在文件夹已存在,如果存在就删除,可是他总是说此图片正由另一进程使用,因此该进程无法访问该文件

解决方案 »

  1.   

    没有释放进程,在Using(){}里面写他自己会给你释放进程
      

  2.   

     if (openFileDialog1.ShowDialog()==DialogResult.OK)
                {
                    try
                    {
                        openFileDialog1.DefaultExt = "jpg";
                        openFileDialog1.Filter = "jpeg图片格式(*.jpeg)|(*.jpeg)|jpg图片格式(*.jpg)|*.jpg|gif图片格式(*.gif)|*.gif|bmp图片格式(*.bmp)|*.bmp|psd图片格式(*.psd)|*.psd|png图片格式(*.png)|*.png";
                        string en = openFileDialog1.SafeFileName.Substring(openFileDialog1.SafeFileName.IndexOf("."));
                       GetFaceImg();
                       if (faceUrl != "")
                       { 
                           File.Delete(faceUrl);
                       }
                       string url = "";
                        if (stuType== "update")
                        {
                            url = @"F:\项目开发Vss\头像\" + "imgStu" + SutPk + en;//重新命名
                        }
                        else
                            if (stuType == "add")
                            {
                                url = @"F:\项目开发Vss\头像\" + "imgStu" + stuID + en;//重新命名
                            }                    imgurl = url;
                        if (File.Exists(url))
                        {
                            isDelete = 0;
                            filename = openFileDialog1.FileName;
                        }
                        else
                        {
                            isDelete = 1;
                            filename = openFileDialog1.FileName;
                        }
                            
                        
                       
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message) ;
                      
                    }
                    //finally
                    //{
                    //    this.pictureBox1.Image.Dispose();
                    //}
                }
                else
                {
                    
                    return;
                }
      

  3.   

    把什么写到using里面啊拜托说的详细一点谢谢
      

  4.   

    我已经贴了在上面呢那是选择图片
    这是保存的部分代码
     if (stuType == "update")
                    {
                        if (imgurl != "")
                        {
                            faceUrl = imgurl;
                        }
                        UpdateStuinfo();
                        this.pictureBox1.Dispose();
                        
                            if (isDelete == 0)
                            {
                                File.Delete(imgurl);
                                File.Copy(filename, imgurl);
                            }
                            else if (isDelete == 1)
                            {
                                File.Copy(filename, imgurl);
                            }
                        
                    }