C# 例外文件正被另一个进程使用(图像)
我正在打开一个图像,然后尝试删除它,但出现以下错误“C#exception.File正被另一个进程使用” 我将Picturebox设置为空C# 例外文件正被另一个进程使用(图像),c#,image,exception,C#,Image,Exception,我正在打开一个图像,然后尝试删除它,但出现以下错误“C#exception.File正被另一个进程使用” 我将Picturebox设置为空 pictureBox1.Image = null; 但问题仍然存在 //File.Delete(Application.StartupPath + @"\Recursos\Fotos\Tragos\" + nombre + ".jpg");// 我猜你用文件对话框加载图像。“否则您需要编辑代码以使用您的方式打开文件” “或提供更多信息或/和代码” 选择图
pictureBox1.Image = null;
但问题仍然存在
//File.Delete(Application.StartupPath + @"\Recursos\Fotos\Tragos\" + nombre + ".jpg");//
我猜你用文件对话框加载图像。“否则您需要编辑代码以使用您的方式打开文件”
“或提供更多信息或/和代码”
选择图像后,请使用文件流。
他将打开图像,阅读它,然后关闭它。“所以它不会被使用”
我已经将OpenFileDialog dlg=newopenfiledialog()放入;在按钮外部,以便我也可以在删除按钮中获取信息。
那它就完美了
OpenFileDialog dlg = new OpenFileDialog();
private void button2_Click(object sender, EventArgs e)
{
dlg.ShowDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
System.IO.FileStream LoadFile = default(System.IO.FileStream);
LoadFile = new System.IO.FileStream(dlg.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
pictureBox1.Image = System.Drawing.Image.FromStream(LoadFile);
LoadFile.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult Sure = MessageBox.Show("Are you Sure ?", "Delete Image", MessageBoxButtons.YesNo);
if (Sure == DialogResult.Yes)
{
pictureBox1.Image = null;
File.Delete(dlg.FileName);
}
}
那么,你不明白其中的哪一部分?某些进程仍在使用该文件。也许这就是你的过程?你是如何加载图像的?您对图像执行了什么其他处理?顺便说一句,将内容设置为null并没有真正的用处,因为在COM/VB6时代是这样的。