【问题标题】:Cannot delete file because it is being used by another process - VB.net无法删除文件,因为它正在被另一个进程使用 - VB.net
【发布时间】:2016-09-21 08:24:34
【问题描述】:

我在 vb.net 中将 TIFF 文件转换为 .PNG 文件。首先,我将 TIFF 文件保存到特定位置(由 NewFileName 提供)。然后我使用 Bitmap.Save 方法将文件转换为 .PNG。稍后在我的程序中,我尝试删除所有 .tif 文件。但是,我收到一条错误消息,指出这些文件仍在使用中。我已经对此原因进行了一些研究,并且我读到很多错误来自未关闭文件流。但是,我没有在我的程序中使用文件流,所以我认为它是另外一回事。建议的另一种可能性是该文件被打开了两次。我已经搜索了我的代码,我很确定这些文件从未打开过,只能使用bitmap.Save 命令进行保存和访问。我还下载了handle.exe 和进程资源管理器以查找锁定文件的进程。显然,这些文件只有在我使用bitmap.save 命令将它们转换为PNG 时才被程序使用。也许有办法关闭bitmap.Save?任何其他关于添加内容的建议也将不胜感激。

    objApplication.StartCommand(SolidEdgeConstants.AssemblyCommandConstants.AssemblyViewBottomView)
    view = window.View
    view.Fit()
    withoutExt = "C:\Folder" & "\" & shortForms(12) & FileName
    NewFileName = withoutExt & ".tif"
    view.SaveAsImage(NewFileName, Width:=width, Height:=height, Resolution:=resolution, ColorDepth:=colorDepth)
    System.Drawing.Bitmap.FromFile(NewFileName).Save(withoutExt & ".png", System.Drawing.Imaging.ImageFormat.Png)

提前致谢!

【问题讨论】:

  • 你能分享你第一次打开TIFF的代码吗?
  • @robin 很抱歉我的问题具有误导性。我从不直接打开 TIFF,我只是使用 .saveasimage 命令和 bitmap.save 命令访问它。我将编辑问题。
  • @robin 我编辑了帖子并包括我发现文件被 bitmap.save 命令锁定。也许这有帮助。
  • 'FromFile' 方法正在放置锁定 - 在这里讨论您的问题 link。也许您需要执行 FromFile,然后分两步保存,将 FromFile 返回保留在一个变量中,您可以在删除 TIFF 之前处理该变量。
  • 我尝试分成两个步骤。 NewFileName 仍然被锁定,因为我需要将它用作将 TIFF 文件转换为 PNG 的路径。

标签: vb.net bitmap png tiff


【解决方案1】:

我想通了。您只需要一个 using 语句,以便在我删除 NewFileName 之前释放它。我把代码改成了这个,它起作用了:

    view = window.View
    view.Fit()
    withoutExt = ChosenFile & "\" & shortForms(13) & FileName
    NewFileName = withoutExt & ".tif"
    view.SaveAsImage(NewFileName, Width:=width, Height:=height, Resolution:=resolution, ColorDepth:=colorDepth)
    Using tempImage = System.Drawing.Bitmap.FromFile(NewFileName)
        tempImage.Save(withoutExt & ".png", System.Drawing.Imaging.ImageFormat.Png)
    End Using
    My.Computer.FileSystem.DeleteFile(NewFileName)

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-04
    • 2014-05-26
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多