无法删除C#.net windows应用程序中的文件

时间:2014-03-22 10:15:59

标签: c# .net file-io

我首先创建Bitmap图像文件并将其保存到某个临时位置。稍后使用该文件读取BitmapImage对象以将其与其他文件进行比较。一旦完成比较,我想删除文件,但它会抛出另一个进程正在使用该文件的异常。如何删除此文件?

这是我的代码:

private void btnLogin_Click(object sender, EventArgs e)
{
        string strPath = AppDomain.CurrentDomain.BaseDirectory;
        GC.Collect();

        if (txtLoginImage.Text != "")
        {
            string strFileName = txtLoginImage.Text.Substring(txtLoginImage.Text.LastIndexOf('\\') + 1);
            Bitmap MainImg = new System.Drawing.Bitmap(txtLoginImage.Text);

            Bitmap NewImage = ConvertToGrayScale(MainImg);

            NewImage.Save(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\" + strFileName, System.Drawing.Imaging.ImageFormat.Bmp);
            NewImage.Dispose();

            Uri SourceUri = new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\" + strFileName);
            BitmapImage source = new BitmapImage();

            source.UriSource = SourceUri;

            IrisSystem.Class.BLL.User_BLL ubll = new IrisSystem.Class.BLL.User_BLL();
            DataSet dsUserData= ubll.getlist();

            bool isMatchFound = false;

            if (dsUserData != null && dsUserData.Tables.Count > 0)
            {
                foreach (DataRow item in dsUserData.Tables[0].Rows)
                {
                    Uri TargetUri = new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Grayscale\\" + item["GrayScaleImgName"]);
                    BitmapImage Target = new BitmapImage(TargetUri);

                    if (source.IsEqual(Target))
                    {
                        IrisSystem.frmHome frm= new IrisSystem.frmHome();
                        frm.strFullName = item["FullName"].ToString();
                        frm.ShowDialog();
                        Form.ActiveForm.Close();
                        isMatchFound = true;
                        break;
                    }
                    Target = null;
                }

                if (!isMatchFound)
                    MessageBox.Show("Invalid Credential..","Invalid Operation");
            }
            File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\" + strFileName);
        }
        else
            MessageBox.Show("Please select image", "Login Error");
    }

1 个答案:

答案 0 :(得分:1)

您需要确保正确放置Bitmap个对象。

您没有处置MainImg对象。您需要使用using {}块来确保正确放置对象。

替换它:

Bitmap MainImg = new System.Drawing.Bitmap(txtLoginImage.Text);    
Bitmap NewImage = ConvertToGrayScale(MainImg);    
NewImage.Save(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\"
                         + strFileName, System.Drawing.Imaging.ImageFormat.Bmp);
NewImage.Dispose();

有了这个:

using(Bitmap MainImg = new System.Drawing.Bitmap(txtLoginImage.Text))            
using(Bitmap NewImage = ConvertToGrayScale(MainImg))
{
  NewImage.Save(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\" + 
                     strFileName, System.Drawing.Imaging.ImageFormat.Bmp);      
}

编辑:

替换它:

BitmapImage source = new BitmapImage();
source.UriSource = SourceUri;

有了这个:

BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = SourceUri;
source.EndInit();