Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Quick tip for anyone else coming here with a similar problem:</p> <p>Watch out for web synchronization apps such as DropBox. I just spent 2 hours thinking the "using" statement (Dispose pattern) is broken in .NET. </p> <p>I eventually realised that Dropbox is continually reading and writing files in the background, in order to sync them. </p> <p>Guess where my Visual Studio Projects folder is located? Inside the "My Dropbox" folder of course. </p> <p>Therefore as I ran my application in Debug mode, the files it was reading and writing were also continually being accessed by DropBox to be synched with the DropBox server. This caused the locking/access conflicts.</p> <p>So at least I now know that I need to a more robust File Open function (ie TryOpen() that will make multiple attempts). I am surprised it's not already a built-in part of the framework.</p> <p>[Update]</p> <p>Here's my helper function:</p> <pre><code>/// &lt;summary&gt; /// Tries to open a file, with a user defined number of attempt and Sleep delay between attempts. /// &lt;/summary&gt; /// &lt;param name="filePath"&gt;The full file path to be opened&lt;/param&gt; /// &lt;param name="fileMode"&gt;Required file mode enum value(see MSDN documentation)&lt;/param&gt; /// &lt;param name="fileAccess"&gt;Required file access enum value(see MSDN documentation)&lt;/param&gt; /// &lt;param name="fileShare"&gt;Required file share enum value(see MSDN documentation)&lt;/param&gt; /// &lt;param name="maximumAttempts"&gt;The total number of attempts to make (multiply by attemptWaitMS for the maximum time the function with Try opening the file)&lt;/param&gt; /// &lt;param name="attemptWaitMS"&gt;The delay in Milliseconds between each attempt.&lt;/param&gt; /// &lt;returns&gt;A valid FileStream object for the opened file, or null if the File could not be opened after the required attempts&lt;/returns&gt; public FileStream TryOpen(string filePath, FileMode fileMode, FileAccess fileAccess,FileShare fileShare,int maximumAttempts,int attemptWaitMS) { FileStream fs = null; int attempts = 0; // Loop allow multiple attempts while (true) { try { fs = File.Open(filePath, fileMode, fileAccess, fileShare); //If we get here, the File.Open succeeded, so break out of the loop and return the FileStream break; } catch (IOException ioEx) { // IOExcception is thrown if the file is in use by another process. // Check the numbere of attempts to ensure no infinite loop attempts++; if (attempts &gt; maximumAttempts) { // Too many attempts,cannot Open File, break and return null fs = null; break; } else { // Sleep before making another attempt Thread.Sleep(attemptWaitMS); } } } // Reutn the filestream, may be valid or null return fs; } </code></pre>
 

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