<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Best practice to check if a folder exists in .NET? in Dropbox API Support &amp; Feedback</title>
    <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Best-practice-to-check-if-a-folder-exists-in-NET/m-p/291144#M17796</link>
    <description>&lt;P&gt;That's so great of you. Thank you so much.&lt;/P&gt;</description>
    <pubDate>Tue, 14 Aug 2018 15:22:28 GMT</pubDate>
    <dc:creator>JayPatel1992</dc:creator>
    <dc:date>2018-08-14T15:22:28Z</dc:date>
    <item>
      <title>Best practice to check if a folder exists in .NET?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Best-practice-to-check-if-a-folder-exists-in-NET/m-p/206163#M10010</link>
      <description>&lt;P&gt;Hi there!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm asking myself what's the best practice to check if a folder exists, because I made a fudge in the meanwhile and the performance is not optimal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm giving the option to our users to overwrite or not the desired uploading file. I actually perform a&amp;nbsp;DropboxClient.Files.SearchAsync with a try catch and if it fails I enable a flag which, if it's enabled, it writes the folder/file (of course it creates automatically the folder). If the flag is not enabled, it &lt;STRONG&gt;searches again&lt;/STRONG&gt; (with this performance losing) overwriting the file or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've&amp;nbsp;found that DropboxClient.Files.GetMetadataAsync is the recommended one, but if the folder doesn't exist it basically throws an Exception, so it's working by now the same way my SearchAsync does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm quite sure there is another way to do this in a better way and I already searched through the Internet, but with no convincent result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Gonzo345.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have just made some refactoring and making some research on the customs Exceptions the API has, so firstly searches and if it goes ok, then proceeds. If it gets the APIException&amp;lt;SearchError&amp;gt; then launches the UploadAsync,&amp;nbsp;creating the folder and uploading the file anyway &lt;img class="lia-deferred-image lia-image-emoji" src="https://www.dropboxforum.com/html/@FBF7D2AB59A0D6E861EBF6A36F93B7E2/emoticons/1f642.png" alt=":slightly_smiling_face:" title=":slightly_smiling_face:" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;try
{
    var taskBusqueda = Task.Run(async () =&amp;gt; await dropbox.Files.SearchAsync(carpetaCloud, nombreArchivo, 0, 100, SearchMode.Filename.Instance));
    taskBusqueda.Wait();

   //If finds a coincidence, acts depending&lt;BR /&gt;   //on how we concreted
   if (taskBusqueda.Result.Matches.Count == 1 &amp;amp;&amp;amp;
           tipoSobreescritura == Tipos.TipoSobreescritura.No)
       //We don't allow overwriting, abort operation&lt;BR /&gt;       throw new SobreescrituraNoPermitidaException("No se ha permitido la sobreescritura del archivo");

   carpetaCloud = carpetaCloud + "/" + nombreArchivo;
   //Upload the file, overwriting by default
   var taskSubidaSobreescribir = Task.Run(async () =&amp;gt; await dropbox.Files.UploadAsync(carpetaCloud, body: fileStream));
   taskSubidaSobreescribir.Wait();&lt;BR /&gt;}
 catch(AggregateException ex) 
 when (ex.InnerException is ApiException&amp;lt;SearchError&amp;gt;)
 {
    //If the search fails, the folder doesn't exist, 
    //so proceed creating the folder and uploading the file
    carpetaCloud = carpetaCloud + "/" + nombreArchivo;
    var taskSubida = Task.Run(async () =&amp;gt; await dropbox.Files.UploadAsync(carpetaCloud, body: fileStream));
    taskSubida.Wait();&lt;BR /&gt;}
return GenerarLink(carpetaCloud);&lt;/PRE&gt;
&lt;P&gt;There might be a better method, but this "works" by now lol&amp;nbsp;&lt;img class="lia-deferred-image lia-image-emoji" src="https://www.dropboxforum.com/html/@8BFF634FABEFCD8860E49834D32E544B/images/emoticons/1f601.png" alt=":grin:" title=":grin:" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 09:26:00 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Best-practice-to-check-if-a-folder-exists-in-NET/m-p/206163#M10010</guid>
      <dc:creator>Gonzo345</dc:creator>
      <dc:date>2019-05-29T09:26:00Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice to check if a folder exists in .NET?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Best-practice-to-check-if-a-folder-exists-in-NET/m-p/206221#M10016</link>
      <description>We recommend using GetMetadataAsync to check if any particular file or folder already exists. It will return the metadata if so, or raise an exception you can catch if not.&lt;BR /&gt;&lt;BR /&gt;GetMetadataAsync would also be better than SearchAsync as searching is subject to an indexing delay.&lt;BR /&gt;&lt;BR /&gt;In either case though, there's going to be a race condition between checking (either via metadata or search) and then uploading. That being the case, check out the write mode options for uploading which offer different ways of handling conflicts.</description>
      <pubDate>Fri, 10 Feb 2017 18:16:50 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Best-practice-to-check-if-a-folder-exists-in-NET/m-p/206221#M10016</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2017-02-10T18:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice to check if a folder exists in .NET?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Best-practice-to-check-if-a-folder-exists-in-NET/m-p/290877#M17783</link>
      <description>&lt;P&gt;Can I&amp;nbsp;have much brief description of the solution and also example related to it if possible. Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 21:24:31 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Best-practice-to-check-if-a-folder-exists-in-NET/m-p/290877#M17783</guid>
      <dc:creator>JayPatel1992</dc:creator>
      <dc:date>2018-08-13T21:24:31Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice to check if a folder exists in .NET?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Best-practice-to-check-if-a-folder-exists-in-NET/m-p/291140#M17794</link>
      <description>&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/760304"&gt;@JayPatel1992&lt;/a&gt;&amp;nbsp;I see you opened a new thread with more details, so I'll follow up there:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.dropboxforum.com/t5/API-Support-Feedback/How-to-check-folder-exist-or-not-and-how-to-upload-image-using/m-p/291082#M17792&amp;nbsp;" target="_blank"&gt;https://www.dropboxforum.com/t5/API-Support-Feedback/How-to-check-folder-exist-or-not-and-how-to-upload-image-using/m-p/291082#M17792&amp;nbsp;&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2018 15:21:07 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Best-practice-to-check-if-a-folder-exists-in-NET/m-p/291140#M17794</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2018-08-14T15:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice to check if a folder exists in .NET?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Best-practice-to-check-if-a-folder-exists-in-NET/m-p/291144#M17796</link>
      <description>&lt;P&gt;That's so great of you. Thank you so much.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2018 15:22:28 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Best-practice-to-check-if-a-folder-exists-in-NET/m-p/291144#M17796</guid>
      <dc:creator>JayPatel1992</dc:creator>
      <dc:date>2018-08-14T15:22:28Z</dc:date>
    </item>
  </channel>
</rss>

