我正在从openfiledialoge中选择文件并将其显示在图片框中,当我点击delete按钮时,它在文本框中显示我正在异常The process cannot access the file because it is being used by another process.
我搜索了很多这个例外以便得到解决,但是当我尝试关闭时,我没有解决任何问题带有imagename的文件,它位于文本框中,即我在图片框中显示的文件; 使用IsFileLocked方法,这会关闭并删除特定目录路径的所有文件,但是如何删除picturebox中显示的唯一文件,我哪里出错了
public partial class RemoveAds : Form
{
OpenFileDialog ofd = null;
string path = @"C:\Users\Monika\Documents\Visual Studio 2010\Projects\OnlineExam\OnlineExam\Image\"; // this is the path that you are checking.
public RemoveAds()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (System.IO.Directory.Exists(path))
{
ofd = new OpenFileDialog();
ofd.InitialDirectory = path;
DialogResult dr = new DialogResult();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Image img = new Bitmap(ofd.FileName);
string imgName = ofd.SafeFileName;
txtImageName.Text = imgName;
pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
ofd.RestoreDirectory = true;
}
}
else
{
return;
}
}
private void button2_Click(object sender, EventArgs e)
{
//Image img = new Bitmap(ofd.FileName);
string imgName = ofd.SafeFileName;
if (Directory.Exists(path))
{
var directory = new DirectoryInfo(path);
foreach (FileInfo file in directory.GetFiles())
{ if(!IsFileLocked(file))
file.Delete();
}
}
}
public static Boolean IsFileLocked(FileInfo path)
{
FileStream stream = null;
try
{ //Don't change FileAccess to ReadWrite,
//because if a file is in readOnly, it fails.
stream = path.Open ( FileMode.Open, FileAccess.Read, FileShare.None );
}
catch (IOException)
{ //the file is unavailable because it is:
//still being written to or being processed by another thread
//or does not exist (has already been processed)
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助
(先前)接受的这个问题的答案是非常糟糕的做法.如果你阅读文档上System.Drawing.Bitmap,特别是创建从文件中位图超载,你会发现:
文件保持锁定状态,直到处理掉位图.
在您的代码中,您创建位图并将其存储在本地变量中,但是在完成后您永远不会将其丢弃.这意味着您的图像对象已超出范围,但尚未对您尝试删除的图像文件释放锁定.对于所有实现的对象IDisposable(如Bitmap),您必须自己处理它们.例如,请查看此问题(或搜索其他人 - 这是一个非常重要的概念!).
要正确纠正问题,您只需在完成后处理图像:
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Image img = new Bitmap(ofd.FileName); // create the bitmap
string imgName = ofd.SafeFileName;
txtImageName.Text = imgName;
pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
ofd.RestoreDirectory = true;
img.Dispose(); // dispose the bitmap object
}
Run Code Online (Sandbox Code Playgroud)
请不要接受以下答案中的建议 - 您几乎不需要打电话GC.Collect,如果您需要这样做以使工作正常,那么这应该是一个非常强烈的信号,表明您正在做其他错误的事情.
此外,如果您只想删除一个文件(您显示的位图),则删除代码错误,并且还会删除目录中的每个文件(这只是重复Adel的观点).此外,OpenFileDialog我不建议只保存一个全局对象来存储文件名,而是建议摆脱它并保存文件信息:
FileInfo imageFileinfo; //add this
//OpenFileDialog ofd = null; Get rid of this
private void button1_Click(object sender, EventArgs e)
{
if (System.IO.Directory.Exists(path))
{
OpenFileDialog ofd = new OpenFileDialog(); //make ofd local
ofd.InitialDirectory = path;
DialogResult dr = new DialogResult();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Image img = new Bitmap(ofd.FileName);
imageFileinfo = new FileInfo(ofd.FileName); // save the file name
string imgName = ofd.SafeFileName;
txtImageName.Text = imgName;
pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
ofd.RestoreDirectory = true;
img.Dispose();
}
ofd.Dispose(); //don't forget to dispose it!
}
else
{
return;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在第二个按钮处理程序中,您可以删除您感兴趣的文件.
private void button2_Click(object sender, EventArgs e)
{
if (!IsFileLocked(imageFileinfo))
{
imageFileinfo.Delete();
}
}
Run Code Online (Sandbox Code Playgroud)