无法在c# .net windows应用程序中删除文件

本文关键字:应用程序 删除 文件 windows net | 更新日期: 2023-09-27 17:51:22

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

无法在c# .net windows应用程序中删除文件

您需要确保您的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();