Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle IOException FileStream Read/Write Timeout and prevent C# application crash
    primarykey
    data
    text
    <p>I have a routine that writes a bunch of data to a file using a filestream; this routine is run after the users click 'Save' in a SaveFileDialog and they select a file location. There is validation as well to make sure the fileName is not empty etc.</p> <p>This data is being written to a file location on a removable drive (SD card) and if I click the button and remove the SD card as quickly as possible (unlikely in practice but found this while stress testing my software) the code enters my catch block and displays the appropriate error message; all good so far.</p> <p>The problem is in addition to my message box displaying the error, windows also comes up with an error message (sometimes 2) saying either 'DW20.exe No Disk in E:\' or 'Please insert a device into Device\Harddisk 1\DR4'. And after this my program crashes which is of course a problem because I want to handle the error in such a way that my program doesn't crash.</p> <p>Below is my save routine.</p> <p>Thanks in advance! Wesley</p> <pre><code> public static void createBinaryFile(string fileName) //build settings file from Local vars and saves to specified file { FileStream fs; try { fs = File.Create(fileName); } catch { MessageBox.Show("The file you selected has failed to save correctly; if your SD card fails to read or write properly you may need to repair it or format it using Windows.", "File Save Failure", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } try { DateTime CurrTime = DateTime.Now; byte u1, u2; // 8 values for system type, unitid, date fs.WriteByte((byte)Global.currentID); // Agent, Disp, DT u1 = (byte)((Local.DeviceSettings.systemID &gt;&gt; 8) &amp; 0xff); u2 = (byte)(Local.DeviceSettings.systemID &amp; 0xff); fs.WriteByte(u1); fs.WriteByte(u2); fs.WriteByte((byte)CurrTime.Month); fs.WriteByte((byte)CurrTime.Day); ... Many more byte of data are written (around 70,000) ... fs.Close(); } catch (IOException ex) { MessageBox.Show("The file you selected has failed to save correctly; if your SD card fails to read or write properly you may need to repair it or format it using Windows.", "File Save Failure", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload