无法删除文件,因为它正被另一个进程使用 - VB.net

时间:2016-05-24 08:30:02

标签: vb.net bitmap png tiff

我正在将一个TIFF文件转换为vb.net中的.PNG文件。首先,我将TIFF文件保存到特定位置(由NewFileName给出)。然后我使用Bitmap.Save方法将文件转换为.PNG。稍后在我的程序中,我尝试删除所有.tif文件。但是,我收到一个错误,指出文件仍在使用中。我已经做了一些关于其原因的研究,我已经读过很多错误来自不关闭文件流。但是,我不在我的程序中使用filestream,所以我认为它是另一回事。建议的另一种可能性是文件被打开了两次。我已经扫描了我的代码,我很确定文件从未打开过,只能使用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)

提前致谢!

1 个答案:

答案 0 :(得分:0)

我明白了。您只需要一个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)

感谢大家的帮助!

相关问题