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

hbuuutag's avatar
hbuuutag
Explorer | Level 4
5 years ago

.Net/C# - getting list of files and then downloading to local drive

I'm back again!

public class Program
    {
        public Logger logger;
        public string localFilePath = @"C:\Users\hbuuutaag\Documents\Code Tests\Test";

            
        public static void Main(string[] args)
        {
            Program program = new Program();
            var task = Task.Run((Func<Task>)program.Run);
            task.Wait();
            
        }

        public async Task Run()
        {
                      
            using (var dbx = new DropboxClient("API TOKEN"))
            {
                logger = NLog.LogManager.GetLogger("logger");

                var full = await dbx.Users.GetCurrentAccountAsync();
                logger.Info("{0} - {1}", full.Name.DisplayName, full.Email);

                var listOfFolders = await dbx.Files.ListFolderAsync(string.Empty, true);

                FolderMetadata dropboxFolderInfo = new FolderMetadata();
                FileMetadata dropboxFileInfo = new FileMetadata();


                foreach (var item in listOfFolders.Entries)
                {
                    try
                    {
                        if (item.IsFile)
                        {
                            dropboxFileInfo = (FileMetadata)item;
                            
                            logger.Info("Processing " + dropboxFileInfo.PathLower.ToString());
                            
                            using (var response = await dbx.Files.DownloadAsync(dropboxFileInfo.PathLower.ToString()))
                            {
                                using (FileStream fileStream = File.Create(localFilePath))
                                {
                                    (await response.GetContentAsStreamAsync()).CopyTo(fileStream);
                                    
                                    logger.Info("Downloaded " + dropboxFileInfo.PathLower.ToString() + " successfully");
                                    
                                }

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.ToString() + "\t" + dropboxFileInfo);
                    }
                }

I'm iterating through the list of folders successfully, but when I try to pass the file name in to the download handler it's downloading one document with Wingdings galore! Probably missing something super basic...

Thanks in advance!

  • I see you've already found the methods for downloading a file, and it sounds like you are getting the file data successfully. If the download call failed it would raise an exception.

    It sounds like you're having an issue viewing the file though, perhaps due to reading it with an incorrect encoding. How are you viewing/accessing the downloaded file? Depending on the file type/encoding you may need to open it with a different application and/or encoding setting. (I can't offer support for configuring third party applications though of course.)

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    I see you've already found the methods for downloading a file, and it sounds like you are getting the file data successfully. If the download call failed it would raise an exception.

    It sounds like you're having an issue viewing the file though, perhaps due to reading it with an incorrect encoding. How are you viewing/accessing the downloaded file? Depending on the file type/encoding you may need to open it with a different application and/or encoding setting. (I can't offer support for configuring third party applications though of course.)

    • hbuuutag's avatar
      hbuuutag
      Explorer | Level 4

      Wasn't sure what I was doing incorrectly but managed to get it sorted - the issue was using the .PathLower rather than .Name attributes.

      Thanks again for the quick response Greg! :)

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,910 PostsLatest Activity: 3 days ago
333 Following

If 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!