Results 1 to 7 of 7

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

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2020
    Posts
    3

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

    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.

  2. #2
    Lively Member
    Join Date
    Jun 2017
    Posts
    77

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

    Did you try FileShare.ReadWrite ?

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2020
    Posts
    3

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

    Quote Originally Posted by BlackRiver1987 View Post
    Did you try FileShare.ReadWrite ?
    I did. Still with the same results.

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

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

    Quote Originally Posted by MoxAssault517 View Post
    I did. Still with the same results.
    Could you post the code for the AppendFile method as well?

    ALso it might be worth seeing if you can identify what process also has the file open, https://docs.microsoft.com/en-us/sys...ocess-explorer can help with that.

    Also, instead of creating a FileInfo object each time you want to get the length you could just use the File class, not helpful with your problem but it is more efficient as it saves creating an instance of FileInfo that you are only using once.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2020
    Posts
    3

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

    Quote Originally Posted by PlausiblyDamp View Post
    Could you post the code for the AppendFile method as well?

    ALso it might be worth seeing if you can identify what process also has the file open, https://docs.microsoft.com/en-us/sys...ocess-explorer can help with that.

    Also, instead of creating a FileInfo object each time you want to get the length you could just use the File class, not helpful with your problem but it is more efficient as it saves creating an instance of FileInfo that you are only using once.

    Code:
        '------------------------------------------------------------------------------------------------------------------------------
        '--- *** APPEND FILE
        '------------------------------------------------------------------------------------------------------------------------------
        Public Shared Function appendFile(ByVal oFile As String, ByVal iFile As String)
            Using fout As Stream = New FileStream(oFile, FileMode.Append)
                Dim newBytes = File.ReadAllBytes(iFile)
                Dim i As Integer = 0
    
                For i = 0 To newBytes.Length - 1
                    fout.WriteByte(newBytes(i))
                Next
            End Using
        End Function

    Update #1:
    While running a cycle through all of the open processes and outputting them into the Debug screen, I could create every file with absolutely no issues. The extra time it took to run through every open process was apparently enough of a delay for the file to close out on its own. Now, how can I go about doing this through code?
    Last edited by MoxAssault517; Nov 16th, 2020 at 01:06 AM.

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

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

    Quote Originally Posted by MoxAssault517 View Post
    [CODE]
    Update #1:
    While running a cycle through all of the open processes and outputting them into the Debug screen, I could create every file with absolutely no issues. The extra time it took to run through every open process was apparently enough of a delay for the file to close out on its own. Now, how can I go about doing this through code?
    That is interesting, I have never known a delay in a file lock being removed when closing a FileStream, perhaps something else is also holding onto the lock - are you running any Anti-Virus programs?

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

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

    Quote Originally Posted by MoxAssault517 View Post
    Code:
        '------------------------------------------------------------------------------------------------------------------------------
        '--- *** APPEND FILE
        '------------------------------------------------------------------------------------------------------------------------------
        Public Shared Function appendFile(ByVal oFile As String, ByVal iFile As String)
            Using fout As Stream = New FileStream(oFile, FileMode.Append)
                Dim newBytes = File.ReadAllBytes(iFile)
                Dim i As Integer = 0
    
                For i = 0 To newBytes.Length - 1
                    fout.WriteByte(newBytes(i))
                Next
            End Using
        End Function

    Update #1:
    While running a cycle through all of the open processes and outputting them into the Debug screen, I could create every file with absolutely no issues. The extra time it took to run through every open process was apparently enough of a delay for the file to close out on its own. Now, how can I go about doing this through code?
    This is going to sound strange... but leave it in... try commenting it out and see if still works or not... but debug statements in running code shouldn't be a problem...

    Quote Originally Posted by PlausiblyDamp View Post
    That is interesting, I have never known a delay in a file lock being removed when closing a FileStream, perhaps something else is also holding onto the lock - are you running any Anti-Virus programs?
    Here's a real-life story for you ... back in the day... VB6... 95 I think it was... had some code that was causing an issue... added some msgboxes to see where it was going... problem went away... added the msgboxes back in... problem disappears... this time I then left them in, but commented them out... code worked... I've got code bases out there that for what ever reason won't run w/o a commented out debug print statement... and it has to be an action comment... it can't be a
    Code:
    'random comment to keep the code working, no, it has to be:
    'msgbox("Foo")

    another time, another life... more code not working... somehow, purely by accident we found that by adding
    Code:
    Dim x as integer
    x = 1
    Solved the problem....

    became a running joke for years...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width