繁体   English   中英

C# File.Delete,另一个进程正在使用的文件

[英]C# File.Delete, file being used by another process

我在尝试删除图像文件时遇到问题。 我总是收到一条错误消息:IOExeption 未处理。 访问被拒绝,因为该文件正被另一个进程使用。

我不知道这可能是什么过程以及如何解决它。

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {            
            Album album = GetAlbum(comboBox1.SelectedIndex);
            Photo photo = GetPhoto(comboBox1.SelectedIndex, comboBox3.SelectedIndex);           

            txtPhotoPath.Text = Directory.GetCurrentDirectory() + "\\"  + photo.SPath;

            lblExtention.Text = photo.SExtention;
            txtPhotoTitle.Text = photo.STitle;
            pctrbFoto.Image = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr());
        }

private void btnChangePhoto_Click(object sender, EventArgs e)
{
            Album album = GetAlbum(comboBox1.SelectedIndex);
            Photo photo = GetPhoto(comboBox1.SelectedIndex, comboBox3.SelectedIndex);

            File.Delete("Albums\\Images\\" + photo.STitle + foto.SExtention);

            photo.SExtention = lblExtention.Text;
            photo.STitle = txtPhotoTitel.Text;
            Photo.SPath = txtPath.Text;

            File.Copy(photo.SPath, "Albums\\Images\\" + photo.STitle + photo.SExtention);

}

谢谢,文兹森特


感谢大家的帮助。

我用过这个,现在效果很好


您的进程是使用 file 的进程,您需要将图像设置为 null 使用以下内容:

var img = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr());

pcrtrbFoto.Image = img;

图像 = 空;

GC.Collect();

我要查看的第一个区域是您的 GetPhoto 方法。 您是否有尚未关闭的 StreamReader? 请确保如果您在删除之前对文件进行任何类型的 I/O,请先关闭这些连接。 GetPhoto() 方法有什么作用?

首先,您需要确定是您的应用程序还是另一个打开了该文件的应用程序。

您可以使用 Mark Russinovich 的 Process Explorer 查看哪个程序打开了特定的文件或目录。 它是 Windows Sysinternals 优秀实用程序系列的一部分,每个程序员/IT 专业人员都应该使用(或至少知道)。

你可以在这里得到它: http : //technet.microsoft.com/en-us/sysinternals/bb896653.aspx

您在哪里使用缩略图:

using(Image img = Image.FromFile(foto.SPath))
{
  pctrbPhoto. Image = img.GetThumbnailImage(
  GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), 
  GetfHeight(photo.SPath, 150), null, new IntPtr()); 
}

而是确保在您完成处理后处理(关闭)源图像。

他们以您的方式拥有它,从文件加载的图像会一直存在,直到垃圾收集器决定释放它,这可能需要一些时间。

用 FromFile 加载的图像保存它们从打开加载的文件。

当您在comboBox3_SelectedIndexChanged调用Image.FromFile时,也可能在其他地方调用Image.FromFile时,您不会处理Image对象。 因此,您的程序正在使用该文件。

每次打开图像时都需要处理图像。

您的进程是使用 file 的进程,您需要将图像设置为 null 使用以下内容:

using(var img = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr()))
  pctrbFoto.Image = img;

当所有其他方法都失败时,您可以使用MoveFileEx在下次重新启动时删除该文件。

...但是,如果您的应用程序在网络托管计划上运行? 您不能在共享服务器中运行任何软件。

我已经尝试过使用 dispose() 和其他选项,但我无法删除像 Vinzcent 这样的文件。

马尔迪托 IIS :@

尝试从同一软件中删除/修改以前保存的文件时会出现此问题。 我通过添加以下代码解决了它:

System.GC.Collect(); 
System.GC.WaitForPendingFinalizers();


File.Delete(MyFile);

您可以使用 Unlocker 程序来告诉您哪些程序锁定了文件

注意:删除了解锁程序的链接 - 包含恶意软件。

我曾经使用过类似thestarSadegh发布的内容,但在某些情况下它不起作用/有帮助,所以我找到了一个不同的解决方案,我已经在这里发布了

仍然在这里代码(您可能会在观看链接和问题后更好地理解它):

   var imageAsByteArray = File.ReadAllBytes(imagePath);

   // I use as example a pictureBox:
   pictureBox1.Image = byteArrayToImage(imageAsByteArray);

   // Or/and safe/copy/replace it:
   File.WriteAllBytes(picture_Path, imageAsByteArray);

您也可以立即删除(新)图片! (如果你想)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM