CodeMonger
CodeMonger

Reputation: 347

VB.net Delete a File after Creating Byte

When I try to delete a Tiff file after creating a byte I get an error stating that I cannot access file because it is being accessed by another process. I am Printing the Byte after the fact if that matters.

                Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(strFile)
                Dim imageConverter As New ImageConverter()
                Dim Bytes As Byte() = DirectCast(imageConverter.ConvertTo(image, GetType(Byte())), Byte())

                Dim PageSettings As New PageSettings
                Dim FS As New FileStream("C:\clean\Packing\output.TIF", FileMode.Create)
                FS.Write(Bytes, 0, Bytes.Length)
                FS.Close()
                FS.Dispose()
                IO.File.Delete(strFile)

Upvotes: 0

Views: 89

Answers (1)

Ivan-San
Ivan-San

Reputation: 843

Try to implement the Using Statement to dispose the object after you finish using it.

The End Using statement disposes of the resources under the Using block's control.

Using image = System.Drawing.Image.FromFile(strFile)
    Dim imageConverter As New ImageConverter() 
    Dim Bytes As Byte() = DirectCast(imageConverter.ConvertTo(image, GetType(Byte())), Byte())
    Dim pageSettings = New PageSettings
    Using FS = New FileStream("C:\clean\Packing\output.TIF", FileMode.Create)
        FS.Write(Bytes, 0, Bytes.Length)
    End Using
End Using
IO.File.Delete(strFile)

Upvotes: 2

Related Questions