cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Tell us what you want to see on the Community here!
Close

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

Highlighted

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

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
Highlighted

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

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
Highlighted

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

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

Highlighted

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

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!

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?