gpt4 book ai didi

c# - "the process cannot access the file because it is being used by another process"试图删除文件时

转载 作者:行者123 更新时间:2023-11-30 19:42:11 25 4
gpt4 key购买 nike

当一个一个地删除文件时,会生成错误“该进程无法访问该文件,因为在尝试删除文件时它正被另一个进程使用”

代码:对于像这样删除文件有什么建议吗?

      private void DeleteFilesFromDestination()
{
string consolidatedFolder = System.Configuration.ConfigurationManager.AppSettings["path"].ToString();

foreach (String file in ListBoxDeleteFiles.Items)
{
try
{
// delete each selected files from the specified TargetFolder
if (System.IO.File.Exists(consolidatedFolder + @"\" + System.IO.Path.GetFileName(file)))
{
proc.WaitForExit();
System.IO.File.Delete(consolidatedFolder + @"\" + System.IO.Path.GetFileName(file));
}
}
catch (Exception ex)
{
MessageBox.Show("Error Could not Delete file from disk " + ex.Message, "Shipment Instruction",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

}
}

注意:图像将像这样加载到流式布局面板中

 //Open the files to see
private void ListBoxSourceFiles_Click(object sender, EventArgs e)
{
try
{
if (ListBoxSourceFiles.SelectedItem != null || !ListBoxSourceFiles.SelectedItem.Equals(string.Empty))
{
//MessageBox.Show("Selected " + ListBoxSourceFiles.SelectedItem);
PictureBox pb = new PictureBox();
Image loadedImage = null;
loadedImage = Image.FromFile(ListBoxSourceFiles.SelectedItem.ToString());
pb.Height = loadedImage.Height;
pb.Width = loadedImage.Width;
pb.Image = loadedImage;
flowLayoutPanel1.Controls.Clear();
flowLayoutPanel1.Controls.Add(pb);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ship Instruction",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}

最佳答案

您没有具体说明您要删除的文件,但从您的问题来看,您似乎在尝试删除您加载的图像文件。如果是这样,那你就有问题了。 documentation for Image.FromFile说:

The file remains locked until the Image is disposed.

如果您需要删除文件的能力,您需要在加载图像后复制图像,并在您的 PictureBox 中使用该图像。然后你可以处理加载的图像,从而解锁文件。

关于c# - "the process cannot access the file because it is being used by another process"试图删除文件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17922206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com