Menu
Home
Forums
New posts
Search forums
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
C# Community
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Home
Forums
VB.NET
VB.NET General Discussion
SystemIOException
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="stulish" data-source="post: 172448" data-attributes="member: 46875"><p>Hi Guys,</p><p></p><p>I am getting an error on a program i created, the program seems to work great so i started trying to cause problems (as users will generally do), I have found i can get it to crash if i remove the removable USB drive while it is compressing data to it, below is a small program i created to generate an error i have been having within the larger program (if anyone wants to try it and see):</p><p></p><p>It just needs one Button and two labels on a form and System.IO.Compression and System.IO.Compression.FileSystem added as references.</p><p></p><p>[CODE]</p><p>Imports System.Windows.Forms</p><p>Imports System</p><p>Imports System.Runtime.InteropServices</p><p>Imports System.ComponentModel</p><p>Imports System.IO</p><p>Imports System.IO.Compression</p><p>Imports System.IO.DriveInfo</p><p>Imports System.Management</p><p>Imports System.Xml</p><p>Imports System.Threading</p><p>Public Class Form1</p><p> Private WithEvents bw As BackgroundWorker = New BackgroundWorker</p><p> Dim SaveLoc As String = "E:\"</p><p> Dim startedCompress As Boolean = False</p><p> Delegate Sub SetTextCallback([text] As String)</p><p> Private demoThread As Thread = Nothing</p><p> Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click</p><p> Try</p><p> bw.RunWorkerAsync()</p><p></p><p> Catch ex As Exception</p><p> SetText2(DateTime.Now.ToString & " - Could not Compress files1: " & ex.Message)</p><p> bw.CancelAsync()</p><p> bw.Dispose()</p><p> End Try</p><p> End Sub</p><p> Private Sub SetText1(ByVal [text] As String)</p><p></p><p> ' InvokeRequired required compares the thread ID of the</p><p> ' calling thread to the thread ID of the creating thread.</p><p> ' If these threads are different, it returns true.</p><p> If Label1.InvokeRequired Then</p><p> Dim d As New SetTextCallback(AddressOf SetText1)</p><p> Me.Invoke(d, New Object() {[text]})</p><p> Else</p><p> Label1.Text = [text]</p><p> End If</p><p> End Sub</p><p> Private Sub SetText2(ByVal [text] As String)</p><p></p><p> ' InvokeRequired required compares the thread ID of the</p><p> ' calling thread to the thread ID of the creating thread.</p><p> ' If these threads are different, it returns true.</p><p> If Label2.InvokeRequired Then</p><p> Dim d As New SetTextCallback(AddressOf SetText2)</p><p> Me.Invoke(d, New Object() {[text]})</p><p> Else</p><p> Label2.Text = [text]</p><p> End If</p><p> End Sub</p><p></p><p> Private Sub doCompress(ByVal fn As String)</p><p> Try</p><p> If Directory.Exists(Mid$(SaveLoc, 1, 3)) Then</p><p> startedCompress = True</p><p> If Not (fn.Contains("TestAudio")) And fn <> "" Then</p><p> Dim fileName As String = System.IO.Path.GetFileName(fn)</p><p> Dim dirname As String = DateTime.Now.ToString("yyyyMMdd-HHmmss")</p><p> If My.Computer.FileSystem.DirectoryExists(SaveLoc & "\" & dirname) = False Then</p><p> My.Computer.FileSystem.CreateDirectory(SaveLoc & "\" & dirname)</p><p> End If</p><p> Dim wavCompressname = "SND-" & fileName & ".zip"</p><p> Dim wavName = fileName & ".wav"</p><p> 'Do Audio</p><p> If System.IO.File.Exists(SaveLoc & wavCompressname) Then</p><p> System.IO.File.Delete(SaveLoc & wavCompressname)</p><p> End If</p><p> Dim audDir As String = fn & "\"</p><p> Dim ZipDir As String = SaveLoc & dirname & "\" & wavCompressname</p><p> ' Debug.Print("-------------------------------------------------" & fileName & "--------------------------")</p><p> 'Debug.Print(DateTime.Now.ToString & " - Start Compress")</p><p> If File.Exists(ZipDir) Then</p><p> ZipDir = ZipDir.Replace(".zip", "-1.zip")</p><p> ' Debug.Print("file exists - renameing file to: " & ZipDir)</p><p> End If</p><p> Try</p><p> ZipFile.CreateFromDirectory(audDir, ZipDir, CompressionLevel.Optimal, False)</p><p> Catch ex As Exception</p><p> SetText2(DateTime.Now.ToString & " - Zip Error")</p><p> bw.CancelAsync()</p><p> bw.Dispose()</p><p> End Try</p><p> startedCompress = False</p><p> End If</p><p> End If</p><p> Catch dirNotFound As System.IO.DirectoryNotFoundException</p><p> ' Code to handle DirectoryNotFoundException. </p><p> SetText2(DateTime.Now.ToString & " - Directory Not Found: ")</p><p> bw.CancelAsync()</p><p> bw.Dispose()</p><p> Catch fileNotFound As System.IO.FileNotFoundException</p><p> ' Code to handle FileNotFoundException. </p><p> SetText2(DateTime.Now.ToString & " - File Not Found: ")</p><p> bw.CancelAsync()</p><p> bw.Dispose()</p><p> Catch *********** As System.IO.***********Exception</p><p> ' Code to handle ***********Exception. </p><p> SetText2(DateTime.Now.ToString & " - Path Too Long: ")</p><p> bw.CancelAsync()</p><p> bw.Dispose()</p><p> Catch ioEx As System.IO.IOException</p><p> ' Code to handle IOException. </p><p> SetText2(DateTime.Now.ToString & " - IO: ")</p><p> bw.CancelAsync()</p><p> bw.Dispose()</p><p> Catch security As System.Security.SecurityException</p><p> ' Code to handle SecurityException. </p><p> SetText2(DateTime.Now.ToString & " - Security: ")</p><p> bw.CancelAsync()</p><p> bw.Dispose()</p><p> Catch ex As Exception</p><p> SetText2(DateTime.Now.ToString & " - Could not Compress files: " & ex.Message)</p><p> bw.CancelAsync()</p><p> bw.Dispose()</p><p> End Try</p><p> End Sub</p><p></p><p> Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load</p><p> bw.WorkerSupportsCancellation = True</p><p> bw.WorkerReportsProgress = True</p><p></p><p> AddHandler bw.DoWork, AddressOf bw_DoWork</p><p> AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted</p><p> End Sub</p><p></p><p> Private Sub bw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bw.DoWork</p><p> SetText1(DateTime.Now.ToString & " - BW Started")</p><p> Try</p><p> If bw.CancellationPending = True Then</p><p> e.Cancel = True</p><p> Else</p><p> 'do work</p><p> doCompress("C:\Temp\Test")</p><p> End If</p><p> Catch ex As Exception</p><p> Debug.Print(DateTime.Now.ToString & " - Unable to start background worker thread: " & ex.Message)</p><p> bw.CancelAsync()</p><p> bw.Dispose()</p><p> End Try</p><p> End Sub</p><p></p><p> Private Sub bw_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles bw.ProgressChanged</p><p> SetText2("Prog Changed")</p><p> End Sub</p><p></p><p> Private Sub bw_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted</p><p> SetText1(DateTime.Now.ToString & " - BW Complete")</p><p> End Sub</p><p>End Class</p><p>[/CODE]</p><p></p><p>I put some stuff into the <strong>"C:\Temp\Test"</strong> directory, this will be zipped onto the flash drive located in the SaveLoc variable in this case "E:" when the button is clicked, make sure there is enough in the temp\test directory to take a little time to compress, i have about 23MB of images and audio this takes about 4-6 seconds to compress and save the files.</p><p></p><p>Once you click the button wait a second and then remove the USB flash drive (please use a clean drive as i would hate for anything you have on the drive becoming corrupt due to bad removal). I have had to do this 3 or 4 times before it shows the error below:</p><p></p><p>[ATTACH]4323[/ATTACH]</p><p></p><p>The immediate window shows:</p><p></p><p></p><p></p><p>The other error message i can get is still a IOException but the detail is: 'The volume for a file has been externally altered so that the opened file is no longer valid.'</p><p></p><p>I know people shouldn't remove USD brives while they are being written too, but i expect some users will and then moan that the app crashes.</p><p></p><p>I have checked the Exceptions settings in the IDE and nothing is checked:</p><p></p><p>[ATTACH]4324[/ATTACH]</p><p></p><p>so i dont understand how sometimes it can be removed with no error and the try/catch working to catch the problem and other times it crashed the program.</p><p></p><p>Any ideas would be welcome.</p><p></p><p>Regards</p><p></p><p>Stu</p></blockquote><p></p>
[QUOTE="stulish, post: 172448, member: 46875"] Hi Guys, I am getting an error on a program i created, the program seems to work great so i started trying to cause problems (as users will generally do), I have found i can get it to crash if i remove the removable USB drive while it is compressing data to it, below is a small program i created to generate an error i have been having within the larger program (if anyone wants to try it and see): It just needs one Button and two labels on a form and System.IO.Compression and System.IO.Compression.FileSystem added as references. [CODE] Imports System.Windows.Forms Imports System Imports System.Runtime.InteropServices Imports System.ComponentModel Imports System.IO Imports System.IO.Compression Imports System.IO.DriveInfo Imports System.Management Imports System.Xml Imports System.Threading Public Class Form1 Private WithEvents bw As BackgroundWorker = New BackgroundWorker Dim SaveLoc As String = "E:\" Dim startedCompress As Boolean = False Delegate Sub SetTextCallback([text] As String) Private demoThread As Thread = Nothing Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try bw.RunWorkerAsync() Catch ex As Exception SetText2(DateTime.Now.ToString & " - Could not Compress files1: " & ex.Message) bw.CancelAsync() bw.Dispose() End Try End Sub Private Sub SetText1(ByVal [text] As String) ' InvokeRequired required compares the thread ID of the ' calling thread to the thread ID of the creating thread. ' If these threads are different, it returns true. If Label1.InvokeRequired Then Dim d As New SetTextCallback(AddressOf SetText1) Me.Invoke(d, New Object() {[text]}) Else Label1.Text = [text] End If End Sub Private Sub SetText2(ByVal [text] As String) ' InvokeRequired required compares the thread ID of the ' calling thread to the thread ID of the creating thread. ' If these threads are different, it returns true. If Label2.InvokeRequired Then Dim d As New SetTextCallback(AddressOf SetText2) Me.Invoke(d, New Object() {[text]}) Else Label2.Text = [text] End If End Sub Private Sub doCompress(ByVal fn As String) Try If Directory.Exists(Mid$(SaveLoc, 1, 3)) Then startedCompress = True If Not (fn.Contains("TestAudio")) And fn <> "" Then Dim fileName As String = System.IO.Path.GetFileName(fn) Dim dirname As String = DateTime.Now.ToString("yyyyMMdd-HHmmss") If My.Computer.FileSystem.DirectoryExists(SaveLoc & "\" & dirname) = False Then My.Computer.FileSystem.CreateDirectory(SaveLoc & "\" & dirname) End If Dim wavCompressname = "SND-" & fileName & ".zip" Dim wavName = fileName & ".wav" 'Do Audio If System.IO.File.Exists(SaveLoc & wavCompressname) Then System.IO.File.Delete(SaveLoc & wavCompressname) End If Dim audDir As String = fn & "\" Dim ZipDir As String = SaveLoc & dirname & "\" & wavCompressname ' Debug.Print("-------------------------------------------------" & fileName & "--------------------------") 'Debug.Print(DateTime.Now.ToString & " - Start Compress") If File.Exists(ZipDir) Then ZipDir = ZipDir.Replace(".zip", "-1.zip") ' Debug.Print("file exists - renameing file to: " & ZipDir) End If Try ZipFile.CreateFromDirectory(audDir, ZipDir, CompressionLevel.Optimal, False) Catch ex As Exception SetText2(DateTime.Now.ToString & " - Zip Error") bw.CancelAsync() bw.Dispose() End Try startedCompress = False End If End If Catch dirNotFound As System.IO.DirectoryNotFoundException ' Code to handle DirectoryNotFoundException. SetText2(DateTime.Now.ToString & " - Directory Not Found: ") bw.CancelAsync() bw.Dispose() Catch fileNotFound As System.IO.FileNotFoundException ' Code to handle FileNotFoundException. SetText2(DateTime.Now.ToString & " - File Not Found: ") bw.CancelAsync() bw.Dispose() Catch *********** As System.IO.***********Exception ' Code to handle ***********Exception. SetText2(DateTime.Now.ToString & " - Path Too Long: ") bw.CancelAsync() bw.Dispose() Catch ioEx As System.IO.IOException ' Code to handle IOException. SetText2(DateTime.Now.ToString & " - IO: ") bw.CancelAsync() bw.Dispose() Catch security As System.Security.SecurityException ' Code to handle SecurityException. SetText2(DateTime.Now.ToString & " - Security: ") bw.CancelAsync() bw.Dispose() Catch ex As Exception SetText2(DateTime.Now.ToString & " - Could not Compress files: " & ex.Message) bw.CancelAsync() bw.Dispose() End Try End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load bw.WorkerSupportsCancellation = True bw.WorkerReportsProgress = True AddHandler bw.DoWork, AddressOf bw_DoWork AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted End Sub Private Sub bw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bw.DoWork SetText1(DateTime.Now.ToString & " - BW Started") Try If bw.CancellationPending = True Then e.Cancel = True Else 'do work doCompress("C:\Temp\Test") End If Catch ex As Exception Debug.Print(DateTime.Now.ToString & " - Unable to start background worker thread: " & ex.Message) bw.CancelAsync() bw.Dispose() End Try End Sub Private Sub bw_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles bw.ProgressChanged SetText2("Prog Changed") End Sub Private Sub bw_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted SetText1(DateTime.Now.ToString & " - BW Complete") End Sub End Class [/CODE] I put some stuff into the [B]"C:\Temp\Test"[/B] directory, this will be zipped onto the flash drive located in the SaveLoc variable in this case "E:" when the button is clicked, make sure there is enough in the temp\test directory to take a little time to compress, i have about 23MB of images and audio this takes about 4-6 seconds to compress and save the files. Once you click the button wait a second and then remove the USB flash drive (please use a clean drive as i would hate for anything you have on the drive becoming corrupt due to bad removal). I have had to do this 3 or 4 times before it shows the error below: [ATTACH]4323.vB[/ATTACH] The immediate window shows: The other error message i can get is still a IOException but the detail is: 'The volume for a file has been externally altered so that the opened file is no longer valid.' I know people shouldn't remove USD brives while they are being written too, but i expect some users will and then moan that the app crashes. I have checked the Exceptions settings in the IDE and nothing is checked: [ATTACH]4324.vB[/ATTACH] so i dont understand how sometimes it can be removed with no error and the try/catch working to catch the problem and other times it crashed the program. Any ideas would be welcome. Regards Stu [/QUOTE]
Insert quotes…
Verification
Post reply
Home
Forums
VB.NET
VB.NET General Discussion
SystemIOException
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top
Bottom