Getting Cannot access a closed file error
<p>I use the following code to iterate a collection of files that I need to copy from one folder to another. It works fine when the source file exist, but when it does not exist I get</p>

<blockquote>
  <p>System.ObjectDisposedException: Cannot access a closed file. at System.IO.__Error.FileNotOpen() at System.IO.FileStream.get_Position()</p>
</blockquote>

<p>What am I missing here?</p>

<pre><code>For Each itm In listOfFiles
    Try
        If File.Exists(itm.SourcePath + itm.FileName) Then

            Dim cf As New FileStream(itm.SourcePath + itm.FileName, FileMode.Open)
            Dim ct As New FileStream(itm.DestinationPath + itm.FileName, FileMode.Create)
            Dim len As Long = cf.Length - 1
            Dim buffer(1024) As Byte
            Dim byteCFead As Integer
            While cf.Position &lt; len
                byteCFead = (cf.Read(buffer, 0, 1024))
                ct.Write(buffer, 0, byteCFead)
                fileCopyProgressBar.BeginInvoke(New Action(Sub() fileCopyProgressBar.Value = CInt(cf.Position / len * 100)))

            End While
            ct.Flush()
            ct.Close()
            cf.Close()

            itm.FileExsits = True

        Else
            itm.FileExsits = False
        End If

    Catch ex As Exception
        log.Error(ex.Message &amp; "  (unc)")
    End Try
Next
</code></pre>