【问题标题】:Cannot delete file in C#.net windows application无法在 C#.net windows 应用程序中删除文件
【发布时间】:2014-04-01 05:47:27
【问题描述】:

我首先创建位图图像文件并将其保存到某个临时位置。稍后使用该文件读取 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");
    }

【问题讨论】:

  • 确保处理所有位图,例如MainImg没有被处理。考虑使用using 构造而不是调用.Dispose(),也使用Path.Combine 来连接资源的路径。

标签: c# .net file-io


【解决方案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();

【讨论】:

  • 仍然不允许我删除。我认为原因是源对象正在使用该文件,并且在操作后它没有释放该文件。我也尝试将 source=null 设置为源是 BitmapImage 类型的对象,它没有 Dispose() 方法
  • 还是同样的错误。有没有办法强制删除使用过的文件?
猜你喜欢
  • 2010-11-10
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多