cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to know more about how you can find anything and protect everything? Check it out 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# - Check and download/Sync missing files with local files

.NET C# - Check and download/Sync missing files with local files

Nickk888
Explorer | Level 3
Go to solution

Hello everyone!

I'm stuck for 2 days now with the API... I'm creating a game launcher where i need to download(sync), all the files for the game Client from Dropbox to the User.

My goal is to check within the local files if the file from my dropbox account exists, if not, download to the specific folder.

It's kinda frustrating using async tasks... But i need my application to be responsive while checking and downloading files, also using a progressbar showing the specific file that is been downloaded. I don't want to download a Zip file because if i made a small change within the client, i don't want the user to download the whole client again and again...

To check if a file has been modified, i wanted to use the Hashing method, but i have no idea how to use it... So i think it is easier to use the file size as a reference?

After one day i finally did a simple Dropbox files and directory list... It was a pain in the A...

But i noticed it is not possible to async download a file inside the async check? Or am i doing something wrong? I know something about C#, but i had always problems understanding the async function...

So my questions are:
- How can i check if a specific file has been modified in the dropbox directory comparing it with the local files(If it hjas been downloaded before)?
- How can i download the file asynchronously to the same local folder?
- How can i implement a progress bar showing the file name AND the progress in percent(0-100)?

i hope you can help me somehow, i searched for a solution on google before, but i had no luck or i didn't understand the specific solution.
Google Drive is too complicated for me, so i hope i have some luck using Dropbox...

And sorry for my english, i'm no native english speaker!

Oh, yeah, here's my code:

static async Task CheckFiles()
        {
            using (var dbx = new DropboxClient(token))
            {
                var amount = 0;
                var list = await dbx.Files.ListFolderAsync(string.Empty, true, false, false, false, true);
                string directoryPath;

                Debug.WriteLine("Dropbox File lister BEGIN");

                foreach (var item in list.Entries.Where(i => i.IsFolder))
                {
                    Debug.WriteLine("Directory: " + item.AsFolder.PathDisplay);
                    directoryPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + ClientDirectory + item.PathDisplay;
                    Directory.CreateDirectory(directoryPath);
                    amount++;
                }

                foreach (var item in list.Entries.Where(i => i.IsFile))
                {
                    Debug.WriteLine("File: {0} | Directory: {1} | Size: {2} | Hash: {3}", item.Name, Path.GetDirectoryName(item.PathDisplay), item.AsFile.Size, item.AsFile.ContentHash);
                    //That's not working!
                    using (var response = dbx.Files.DownloadAsync(Path.GetDirectoryName(item.PathDisplay) + "/" + item.Name))
                    {
                        Debug.WriteLine(response.GetContentAsStringAsync());
                    }
                    //
                    amount++;
                }

                Debug.WriteLine("Dropbox File lister END - Found: {0} entries", amount);
            }
        }


Best regards!

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

[Cross-linking for reference: https://stackoverflow.com/questions/52625933/net-c-sharp-check-and-download-sync-missing-files-with-... ]

"use the file size as a reference?"

I do not recommend using size, as that can result in false positives. That is, two files can have the same size but different contents.

"How can i check if a specific file has been modified in the dropbox directory comparing it with the local files(If it hjas been downloaded before)?"

One way to do this would be to record which "rev" you downloaded, that is, the FileMetadata.Rev, included with the file download. When you check again later (e.g., from GetMetadata or ListFolder[Continue]), if the rev hasn't changed, you know you still have the latest copy. If the rev has changed, the file data may have changed.

Alternatively, you can check the FileMetadata.ContentHash. That can directly indicate if you have the same file data. You can find information on how that works here.


"How can i download the file asynchronously to the same local folder?"

You can use the DownloadAsync method to download files, as seen in the examples here and here. What you do with the file data is up to you. For instance, unlike those examples, it sounds like you want to save the data to the local filesystem.


"How can i implement a progress bar showing the file name AND the progress in percent(0-100)?"

The Dropbox API v2 .NET SDK does not offer progress listeners, but I'll be sure to pass this along as feedback. You can track progress from the download stream itself though as shown in this thread.

 


View solution in original post

10 Replies 10

Greg-DB
Dropbox Staff
Go to solution

[Cross-linking for reference: https://stackoverflow.com/questions/52625933/net-c-sharp-check-and-download-sync-missing-files-with-... ]

"use the file size as a reference?"

I do not recommend using size, as that can result in false positives. That is, two files can have the same size but different contents.

"How can i check if a specific file has been modified in the dropbox directory comparing it with the local files(If it hjas been downloaded before)?"

One way to do this would be to record which "rev" you downloaded, that is, the FileMetadata.Rev, included with the file download. When you check again later (e.g., from GetMetadata or ListFolder[Continue]), if the rev hasn't changed, you know you still have the latest copy. If the rev has changed, the file data may have changed.

Alternatively, you can check the FileMetadata.ContentHash. That can directly indicate if you have the same file data. You can find information on how that works here.


"How can i download the file asynchronously to the same local folder?"

You can use the DownloadAsync method to download files, as seen in the examples here and here. What you do with the file data is up to you. For instance, unlike those examples, it sounds like you want to save the data to the local filesystem.


"How can i implement a progress bar showing the file name AND the progress in percent(0-100)?"

The Dropbox API v2 .NET SDK does not offer progress listeners, but I'll be sure to pass this along as feedback. You can track progress from the download stream itself though as shown in this thread.

 


mikesam
Explorer | Level 4
Go to solution

Hi Nickk,

 

Do the game launcher work for you?

If yes, do we need to hardcode the token and make it available to all client user?

Thanks and hope to hear from you soon.

 

Best Regards,

Mike

Здравко
Legendary | Level 20
Go to solution

@mikesam wrote:

...

If yes, do we need to hardcode the token and make it available to all client user?

...


Hi @mikesam,

Any kind of authentication info (including token) should be handled with care. Such information should be embedded only when application gonna be used on server side or by responsible person (or limited set of persons). In any other case embedding such information can be considered as a security hole. It can be dangerous for both the authenticated account as well as application users. So, such thing should be performed with extreme care and it's generally discouraged!

mikesam
Explorer | Level 4
Go to solution

Hi @Здравко,

 

Thanks for you reply.

Do you have any better suggestions if I had the same use case as Nickk?

 

The idea was have a game launcher can get up-to-date files from dropbox (specified folder)

Without write access to that token would be fine I guess.

 

Best Regards,

Mike

Здравко
Legendary | Level 20
Go to solution

@mikesam, to be honest, I'm not certain what you are asking actually. Greg's post above describes things in useful manner. Can you clarify what isn't working for you or what exactly you can't understand? 🧐

 

Add: Something missing, both in OP and in Greg's answer too, is at present long lived access token is not issued anymore. This type of token is obsolete, in spite existing can still be used. Its successor is refresh token (for long term access). So, you may consider refresh token usage instead of long-lived access token, if applicable. Keeping the code from OP (or similar) without any change gonna require re-authentication on regular basis (like every 4 hours). This is a recent change.

Greg-DB
Dropbox Staff
Go to solution

@mikesam As Здравко mentioned, it's not recommended to distribute any access tokens for your own account to other users. You can technically now use scopes to restrict an app/access token to only have read and not write access, but it still would be a bad security practice to distribute your access token(s). You may be better served by a more general purpose CDN.

mikesam
Explorer | Level 4
Go to solution

Hi @Здравко 

 

Thanks for your reply.

I'm stucking at the examples, seems that examples no longer work.

And some of the page/link of reply from Greg was broken.

mikesam_0-1662730663028.png

 

 

 

Best Regards,

Mike

Здравко
Legendary | Level 20
Go to solution

Would be fine if you have supplied a reference to the example you are evaluating and the code-line where issues are rising from.

As far as I can see, you are trying to upload something and file content get directed to as metadata instead of as file content (as should be). This comes/can be seen from the error message. 🤔 I don't know what actually does confuse you. Review your code. 😉

Greg-DB
Dropbox Staff
Go to solution

@mikesam My first post in this thread was from several years ago, so some things may have changed since then. You can find the latest resources for the .NET SDK here.

 

If something isn't working as expected, please share the details, such as the steps you're following and the code you're running, so we can take a look.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Nickk888 Explorer | Level 3
  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Здравко Legendary | Level 20
  • User avatar
    mikesam Explorer | Level 4
What do Dropbox user levels mean?