cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
We've been busy working on some major updates to the Community, so check out what's changing, what’s staying the same and what you can expect from the Dropbox Community right here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Best practice to check if a folder exists in .NET?

Best practice to check if a folder exists in .NET?

Gonzo345
Helpful | Level 5
Go to solution

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:

 

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution
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.

View solution in original post

4 Replies 4

Greg-DB
Dropbox Staff
Go to solution
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.

JayPatel1992
Helpful | Level 5
Go to solution

Can I have much brief description of the solution and also example related to it if possible. Thanks.

Greg-DB
Dropbox Staff
Go to solution

@JayPatel1992 I see you opened a new thread with more details, so I'll follow up there:

 

https://www.dropboxforum.com/t5/API-Support-Feedback/How-to-check-folder-exist-or-not-and-how-to-upl...

JayPatel1992
Helpful | Level 5
Go to solution

That's so great of you. Thank you so much.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    JayPatel1992 Helpful | Level 5
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?