Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15628

VS 2019 - System.IO.IOException - Started out of no where

$
0
0
I have been working on a program for about a month that builds multiple files one after another. I use a "template" IMG file that out of the blue just started throwing this error:

System.IO.IOException: 'The process cannot access the file 'myfile.img' because it is being used by another process.'


Code:

        Dim tmpCart = $"{_wb}imgs\byog_cartridge_shfs_twin.img"

            Thread.Sleep(1000)
            '--- IMAGE SIZE (PRE 4K)
            Dim fSize = New FileInfo(tmpCart).Length
            Debug.Write($"{_N}*** Size Pre-4K {fSize} Bytes{_N}")
            Dim realBytes4k = fSize / 4096
            Debug.Write($"*** RealBytes4k Pre Mod {realBytes4k} Bytes{_N}")
            If fSize Mod 4096 <> 0 Then
                Debug.Write($"*** Difference {fSize Mod 4096} Bytes{_N}")
                Dim num() As String = Convert.ToString(realBytes4k).Split(".")
                Dim round4k As Integer = FormatNumber(realBytes4k, 0)
                Debug.Write($"*** num(0): {num(0)} *** num(1): {num(1)} *** Bytes: {round4k}{_N}")
                realBytes4k = num(0) + 1
            End If
            Debug.Write($"*** RealBytes4k {realBytes4k} Bytes{_N}{_N}")

            '--- IMAGE SIZE (POST 4K)
            Dim realBytesUsed = realBytes4k * 4096
            Debug.Write($"*** RealBytesUsed {realBytesUsed} Bytes{_N}")
            Dim padding = Int(realBytesUsed - fSize)
            Debug.Write($"*** Size Post-4K {fSize} Bytes (Padding: {padding} Bytes){_N}")
            Dim nulPad = SpecByteArray(padding)
            Debug.Write($"*** Size of nulPad {nulPad} Bytes{_N}")
            AppBinData(tmpCart, nulPad)
            fSize = New FileInfo(tmpCart).Length
            Debug.Write($"*** Offset of Ext4 {fSize + 64} Bytes{_N}{_N}")

            '--- IMAGE SIZE (ADD MD5 HASH)
            Dim strMD5Hex = getMD5(tmpCart)
            WriteToFile($"{_wb}Tools\md5sum.wintst", strMD5Hex)
            Dim tstMD5 = HexByte(strMD5Hex)
            Write2File($"{md5Img}", tstMD5)
            AppBinData(tmpCart, tstMD5)
            Debug.Write($"*** SQFS MD5 Hash {strMD5Hex} -- {HexByte(strMD5Hex)}{_N}")
            Debug.Write($"*** Size of tstMD5 {tstMD5}{_N}")
            fSize = New FileInfo(tmpCart).Length
            Debug.Write($"*** After MD5 {fSize}{_N}{_N}")

            '--- IMAGE SIZE (ADD RESERVES)
            nulPad = SpecByteArray(32)
            Debug.Write($"*** Size of nulPad {nulPad} Bytes{_N}")
            AppBinData(tmpCart, nulPad)
            Write2File($"{_wb}Tools\reservedspace.wintst", nulPad)
            fSize = New FileInfo(tmpCart).Length
            Debug.Write($"*** After Reserve {fSize} Bytes{_N}{_N}")

            '--- IMAGE SIZE (ADD CARTRIDGE MD5 HASH)
            appendFile(tmpCart, md5Img)
            fSize = New FileInfo(tmpCart).Length
            Debug.Write($"*** After Cart MD5 {fSize} Bytes{_N}{_N}")

            '--- FINAL IMAGE SIZE
            final = $"{_PackD}\{_addon}{_title}.UCE"
            File.Copy(tmpCart, final, True)
            appendFile(final, cartImg)
            fSize = New FileInfo(final).Length
            Debug.Write($"*** Final imgSave {fSize}{_N}{_N}")

            My.Computer.FileSystem.DeleteDirectory(_path, UIOption.OnlyErrorDialogs,
                                                      RecycleOption.DeletePermanently,
                                                      UICancelOption.DoNothing)
            Return "Complete"
        End If
        'Catch ex As Exception
        'Console.WriteLine($"Creation has failed!{_N}{_N}Error Code{_N}({ex.Message})")
        'Return ex.Message
        'End Try
    End Function




    Friend Shared Function AppBinData(ByVal filename As String, ByVal binaryData As Byte())
        Using fout As New FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None)
            Dim i As Integer = 0
            For i = 0 To binaryData.Length - 1
                fout.WriteByte(binaryData(i))
            Next
        End Using
    End Function


I put the spots it will error out in bold, italicized, and underlined. Prior to last week, I was able to create 20-30 files at a time with no issues, and now it will make it to file three(3) and then error out. Any help would be wonderful.

Viewing all articles
Browse latest Browse all 15628

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>