cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Whether you are transferring a single drive, a team or an entire organization, Movebot's cloud migration tool has been built to make your Dropbox migration simple - learn all about it 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: 

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

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

hbuuutag
Explorer | Level 4

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!

1 Accepted Solution

Accepted Solutions

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

Greg-DB
Dropboxer

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.)

View solution in original post

2 Replies 2

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

Greg-DB
Dropboxer

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.)

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

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

Who's talking

Top contributors to this post

  • User avatar
    hbuuutag Explorer | Level 4
  • User avatar
    Greg-DB Dropboxer
What do Dropbox user levels mean?
Need more support?