How to programmatically upload a file to your Dropbox account.
On a train trip during the New Year’s weekend, I spent time testing and collecting notes about how to interact with Dropbox. I will share my findings with you in this blog post.
“Dropbox is a free service that lets you bring your photos, docs, and videos anywhere and share them easily. Never email yourself a file again!” – dropbox.com”
Follow these steps to create a Dropbox account and register the app you will use to interact with your Dropbox file area:
- Create a new Dropbox account at www.dropbox.com/login#register
- Accept the Dropbox App Developer terms at www.dropbox.com/developers/apply
- Continue with the next step, where you create an app with a suitable test name and a short nonsense description
The Dropbox admin interface should now show you the App Key and App Secret you need later for creating a security token file.
My train trip was quite long, so I decided to put another card in the deck. SharpBox is a popular framework for accessing storage and one of the storage service providers it supports is Dropbox. (You can, of course, skip SharpBox and use the Dropbox API directly but I decided out of curiosity to test SharpBox.)
“SharpBox is the right free open source solution for your project. Our goal is to disburden software projects from the demand to implement storage access twice to support a wide range of storage clouds.” – sharpbox.codeplex.com
Follow these steps to set up a test application in Visual Studio 2010 and include Sharpbox in it:
- Create a new C# Console Application in Visual Studio
- Open the properties of the application and set Target Framework to .NET Framework 4
- Add the SharpBox NuGet package named AppLimit.CloudComputing.SharpBox
- Open the File Explorer and start the SharpBox tool DropBoxTokenIssuer.exe located in the folder approotpackagesAppLimit.CloudComputing.SharpBox.184.108.40.2062libnet40-full. This tool is used to create a Security Token file you will use in your application.
- Enter the Application Key and Application Secret you previously got in the Dropbox admin interface. Name the file DropBoxTokenand finally click the Authorize button.
- This will hopefully lead you to the Dropbox login page, where you will use the login credentials for the account you previously created. (Please make sure you set the Target Framework correctly to .Net Framework 4 if no login page appears.)
- If everything worked out correctly, you will now see an error page. Confusing? This is because the Sharpbox tool redirects to a page that does not exist on Codeplex. You will find the Security Token file created in the same folder where the tool application exists.
- Finally, move this Security Token file to C:Test together with an empty text file you create and name texttest.txt.
Everything is now set up for some coding. Fill the Program.cs with this:
static void Main()
var dropBoxStorage = new CloudStorage();
var dropBoxConfig = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox);
// declare an access token
// load a valid security token from file
using (var fs = File.Open(@"C:TestDropBoxToken", FileMode.Open, FileAccess.Read, FileShare.None))
accessToken = dropBoxStorage.DeserializeSecurityToken(fs);
// open the connection
var storageToken = dropBoxStorage.Open(dropBoxConfig, accessToken);
var srcFile = Environment.ExpandEnvironmentVariables(@"C:Testtexttest.txt");
// close the connection
After executing the application, you should be able to see in your Dropbox client of choice or Dropbox admin interface that the file texttest.txt has been uploaded to your Dropbox file area.
Useful Hints and Links
I have now successfully stored files this way for more than a month in a minor hobby application and it works great. After uploading files, you probably also want to download them and SharpBox can help you with this and much more. The only problem I encountered in my own application was when trying to download a file from Dropbox and sending it to the user via a Web browser as a stream. The file had the correct size, but seemed to be empty. The solution was to use Stream.Seek(0,0) before returning the filestream.
You can find more info here:
A lot of response lately reported problems about creating the token file. I have tried to get in contact with the dev team behind SharpBox without success and your questions at the SharpBox forum are leaved without answers. The project might unfortunately be dead, but my suggestion is to continue to use their forum and try to get in contact with the dev team to solve the problem. You are welcome to write a comment here if you have an update about the SharpBox Project status