VB.NET 如何释放打开的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/756039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:10:22  来源:igfitidea点击:

VB.NET How do I release an open file?

vb.net

提问by Adam Robinson

I'm opening a file called tempImage.jpg and showing it on a form in a PictureBox. I then click a button called Clear and the file is removed from the PictureBox using PictureBox2.Image = Nothing, however I'm unable to delete the file as it is locked open. How can I release it so I can delete it? I'm using VB.NET and a forms app.

我正在打开一个名为 tempImage.jpg 的文件,并将其显示在 PictureBox 的表单上。然后我单击一个名为 Clear 的按钮,然后使用 PictureBox2.Image = Nothing 从 PictureBox 中删除该文件,但是我无法删除该文件,因为它被锁定打开。我怎样才能释放它以便我可以删除它?我正在使用 VB.NET 和一个表单应用程序。

Thanks

谢谢

回答by Jon Skeet

When you use PictureBox2.Image = Nothingyou're waiting for the garbage collector to finalize the resource before it releases it. You want to release it immediately, so you need to dispose of the image:

当您使用时,PictureBox2.Image = Nothing您正在等待垃圾收集器在释放资源之前完成资​​源。你想立即释放它,所以你需要处理图像:

Image tmp = PictureBox2.Image
PictureBox2.Image = Nothing
tmp.Dispose()

回答by Adam Robinson

If you're using Image.FromFile, you need to call .Dispose() on the image. When you go to clear it out, do something like...

如果您使用 Image.FromFile,则需要在图像上调用 .Dispose()。当你去清除它时,做一些像......

Image currentImage = pictureBox.Image

pictureBox.Image = Nothing

currentImage.Dispose()

That will release the file.

这将释放文件。

回答by dbasnett

take control of the file

控制文件

    'to use the image
    Dim fs As New IO.FileStream("c:\foopic.jpg", IO.FileMode.Open, IO.FileAccess.Read)
    PictureBox1.Image = Image.FromStream(fs)

    'to release the image
    PictureBox1.Image = Nothing
    fs.Close()

回答by Crash893

is there a equivalent to using in vb.net

是否有等效于在 vb.net 中使用

this is what i would do in c#

这就是我在 C# 中会做的

using( filestream fs = new filestream)
{

//whatever you want to do in here


}

//closes after your done

回答by Ben

As i can't comment yet (not enough experience points), this is an answer for the above "is there a equivalent to using in vb.net"

由于我还不能评论(没有足够的经验点),这是对上述“是否有等效于在 vb.net 中使用”的回答

Yes, in .Net 2.0 and above you can use "Using". In .Net 1.0 and 1.1 however, you would need to dispose if the object in the finally block

是的,在 .Net 2.0 及更高版本中,您可以使用“使用”。但是,在 .Net 1.0 和 1.1 中,如果 finally 块中的对象,您将需要处理

    Dim fs As System.IO.FileStream = Nothing
    Try
        'Do stuff
    Finally
        'Always check to make sure the object isnt nothing (to avoid nullreference exceptions)
        If fs IsNot Nothing Then
            fs.Close()
            fs = Nothing
        End If
    End Try

Adding the closing of the stream in the finally block ensures that it will get closed no matter what (as opposed to the connection getting opened, a line of code bombing out beneath before the stream is closed, and the stream staying open and locking the file)

在 finally 块中添加流的关闭确保它无论如何都会被关闭(与打开连接相反,在流关闭之前在下面炸出一行代码,并且流保持打开状态并锁定文件) )