c# - How can I make sure that a file inside a Dropbox is really gone after delete? -
i using files inside dropbox communicate between servers. after while realized every once in while there delay in basic file
operations when executed on file inside dropbox.
what need this:
public class myfile { const int maxwaitcount = 60; //.. /// <summary> /// remove file - has work inside dropbox directory /// </summary> /// <returns>0 ok or error code</returns> public int rm() { string name = os.wininternal(fullname); if (file.exists(name)) { file.delete(name); #region double check if file gone if (ensure) return awaitfilevanishing(name); #endregion } return 0; } //.. private int awaitfilematerializing(string filename) //.. private int awaitfilevanishing(string filename) //.. /// <summary> /// constructor: auto-ensure mode file systems not synchronously wait end of io operation i.e. dropbox /// </summary> /// <remarks>only use ensure mode if has guaranteed io operation done /// when method call returns; necessary e.g. dropbox directories since (currently) dropbox first updates /// file in invisible . folder , asynchronously updates visible file , remote copies of it</remarks> /// <param name="filename"></param> public myfile(string filename) { ensure = filename.tolower().contains("dropbox"); // .. } }
since needed quick solution, came 1 presenting answer own question below. however, can imagine of had problem , found different, possibly more compelling solution. please let me know.
this current solution awaitfilematerializing
, awaitfilevanishing
used part of myfile
.
private int awaitfilematerializing(string filename) { int count = 0; bool exists = false; while (count < maxwaitcount) { try { exists = file.exists(filename); } catch (exception) { } // device not ready exception if win 2003 if (exists) break; thread.sleep(5); count++; } if (count >= maxwaitcount) throw new filenotfoundexception("ensure failed - timeout.", filename); return -count; } private int awaitfilevanishing(string filename) { int count = 0; bool exists = true; while (count < maxwaitcount) { try { exists = file.exists(filename); } catch (exception) { } // device not ready exception if win 2003 if (!exists) break; thread.sleep(5); count++; } if (count >= maxwaitcount) throw new ioexception("ensure failed - timeout in deleting " + filename + "."); return -count; }
Comments
Post a Comment