C# 删除文件出错

来源:互联网 发布:程序员用的最多的库 编辑:程序博客网 时间:2024/05/22 04:46

1: 做一个项目的时候用到显示图片,开始的代码

 Bitmap bitImg = null;
 //bitImg.Dispose();
 //bitImg = new Bitmap(materialsPicPath);
 //this.pictureBox1.Image = bitImg;

 可是老出错,删除不了文件,

组后代码:

fsImage = new FileStream(filePath, FileMode.Open);
this.pictureBox1.Image = Image.FromStream(fsImage);
fsImage.Close();

分析原因:开始的代码,由于 new Bitmap(materialsPicPath);锁定了资源,如果强制
//bitImg.Dispose(); 会出现毛病,

后面的代码 fsImage.Close(); 用数据流,每一次都关闭数据流 ,就可以安全的删除文件。