Dropbox for .NET Developers

Dropbox.NET

Dropbox.NET is our .NET SDK for API v2, which helps you easily integrate Dropbox into your app. The Dropbox.NET SDK is a Portable Class Library that works with multiple platforms including Windows, Windows Phone, and Mono.

Code

Dropbox.NET - Dropbox.NET is open source on GitHub.

Sample apps

Several examples can be found in the examples directory:

  • SimpleBlogDemo - An ASP.NET MVC application that creates a simple blogging platform and shows how to upload and download files.
  • SimpleBusinessDashboard - An ASP.NET MVC application that creates a simple business dashboard, showcasing the Dropbox Business endpoints.
  • SimpleTest - A windows console application that demonstrates basic use of the SDK; this also contains code that connects with OAuth2 using WPF.
  • UniversalDemo - A slide show app for both Windows Store and Windows Phone 8.1

Install Dropbox.NET

We recommend using NuGet to install the new Dropbox.NET SDK.

To install Dropbox.Api, run the following command in the Package Manager Console:

    PM> Install-Package Dropbox.Api

That's it! Now you're ready to get started with the tutorial.

Dropbox.NET tutorial

A good way to start using Dropbox.NET is to follow this quick tutorial. Just make sure you have the Dropbox.NET SDK installed first!

Register a Dropbox API app

To use the Dropbox API, you'll need to register a new app in the App Console. Select Dropbox API app and choose your app's permission. You'll need to use the app key created with this app to access API v2.

Link an account

In order to make calls to the API, you'll need an instance of the DropboxClient object. To instantiate, pass in the access token for the account you want to link.

You can generate an access token for testing with your own account through the App Console. In order to authorize additional users, the Dropbox.NET SDK contains helper methods for using OAuth with Dropbox.

Here's an example where we set up the DropboxClient and check the current user by calling GetCurrentAccountAsync, which returns an instance of FullAccount:

using System;
using System.Threading.Tasks;
using Dropbox.Api;

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

    static async Task Run()
    {
        using (var dbx = new DropboxClient("YOUR ACCESS TOKEN"))
        {
            var full = await dbx.Users.GetCurrentAccountAsync();
            Console.WriteLine("{0} - {1}", full.Name.DisplayName, full.Email);
        }
    }
}

Try some API requests

Now that you have a DropboxClient handy, let's try making some API requests. You can use the DropboxClient object you instantiated above to make API calls. Let's try out some of the Files requests.

To list all the contents in the user's root directory:

async Task ListRootFolder(DropboxClient dbx)
{
    var list = await dbx.Files.ListFolderAsync(string.Empty);

    // show folders then files
    foreach (var item in list.Entries.Where(i => i.IsFolder))
    {
        Console.WriteLine("D  {0}/", item.Name);
    }

    foreach (var item in list.Entries.Where(i => i.IsFile))
    {
        Console.WriteLine("F{0,8} {1}", item.AsFile.Size, item.Name);
    }
}

To download a file:

async Task Download(DropboxClient dbx, string folder, string file)
{
    using (var response = await dbx.Files.DownloadAsync(folder + "/" + file))
    {
        Console.WriteLine(await response.GetContentAsStringAsync());
    }
}

To upload a file:

async Task Upload(DropboxClient dbx, string folder, string file, string content)
{
    using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(content)))
    {
        var updated = await dbx.Files.UploadAsync(
            folder + "/" + file,
            WriteMode.Overwrite.Instance,
            body: mem);
        Console.WriteLine("Saved {0}/{1} rev {2}", folder, file, updated.Rev);
    }
}

Find out more details in the full documentation.

Dropbox.NET documentation

Here's the full documentation for the Dropbox.NET SDK.