Using Dropbox from code

When I’m writing a utility that keeps some data I like to store it in my dropbox folder so it’s synchronised between all of my computers. This C# code will attempt to find where this folder is stores, or return null if it doesn’t seem to be there because either Dropbox isn’t installed or the internal details of how the location is stored has been changed again.

It checks in two locations because Dropbox changed where this was stored not so long ago and this code should work with both old and new installs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static string GetDropboxFolder() {
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var dbPath = Path.Combine(appDataPath, "Dropbox\host.db");

if(!File.Exists(dbPath)) {
appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
dbPath = Path.Combine(appDataPath, "Dropbox\host.db");
if(!File.Exists(dbPath)) {
return null;
}
}
var lines = File.ReadAllLines(dbPath);
var dbBase64Text = Convert.FromBase64String(lines[1]);
var folderPath = Encoding.ASCII.GetString(dbBase64Text);
return folderPath;
}