You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.
Forum Discussion
Gonzo345
8 years agoHelpful | Level 5
Best practice to check if a folder exists in .NET?
Hi there!
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.
I'm giving the option to our users to overwrite or not the desired uploading file. I actually perform a 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 searches again (with this performance losing) overwriting the file or not.
I've 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.
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.
Thanks,
Gonzo345.
EDIT:
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<SearchError> then launches the UploadAsync, creating the folder and uploading the file anyway :)
try { var taskBusqueda = Task.Run(async () => await dropbox.Files.SearchAsync(carpetaCloud, nombreArchivo, 0, 100, SearchMode.Filename.Instance)); taskBusqueda.Wait(); //If finds a coincidence, acts depending
//on how we concreted if (taskBusqueda.Result.Matches.Count == 1 && tipoSobreescritura == Tipos.TipoSobreescritura.No) //We don't allow overwriting, abort operation
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 () => await dropbox.Files.UploadAsync(carpetaCloud, body: fileStream)); taskSubidaSobreescribir.Wait();
} catch(AggregateException ex) when (ex.InnerException is ApiException<SearchError>) { //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 () => await dropbox.Files.UploadAsync(carpetaCloud, body: fileStream)); taskSubida.Wait();
} return GenerarLink(carpetaCloud);
There might be a better method, but this "works" by now lol :grin:
- 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.
GetMetadataAsync would also be better than SearchAsync as searching is subject to an indexing delay.
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.
- Greg-DBDropbox StaffWe 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.
GetMetadataAsync would also be better than SearchAsync as searching is subject to an indexing delay.
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.- JayPatel1992Helpful | Level 5
Can I have much brief description of the solution and also example related to it if possible. Thanks.
- Greg-DBDropbox Staff
JayPatel1992 I see you opened a new thread with more details, so I'll follow up there:
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,910 PostsLatest Activity: 4 days agoIf you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!