How do I programmatically locate my Dropbox folder using C#?

UPDATED SOLUTION

Dropbox now provides an info.json file as stated here: https://www.dropbox.com/en/help/4584

If you don’t want to deal with parsing the JSON, you can simply use the following solution:

var infoPath = @"Dropbox\info.json";

var jsonPath = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), infoPath);            

if (!File.Exists(jsonPath)) jsonPath = Path.Combine(Environment.GetEnvironmentVariable("AppData"), infoPath);

if (!File.Exists(jsonPath)) throw new Exception("Dropbox could not be found!");

var dropboxPath = File.ReadAllText(jsonPath).Split('\"')[5].Replace(@"\\", @"\");

If you’d like to parse the JSON, you can use the JavaScripSerializer as follows:

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();            

var dictionary = (Dictionary < string, object>) serializer.DeserializeObject(File.ReadAllText(jsonPath));

var dropboxPath = (string) ((Dictionary < string, object> )dictionary["personal"])["path"];

DEPRECATED SOLUTION:

You can read the the dropbox\host.db file. It’s a Base64 file located in your AppData\Roaming path. Use this:

var dbPath = System.IO.Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Dropbox\\host.db");

var dbBase64Text = Convert.FromBase64String(System.IO.File.ReadAllText(dbPath));

var folderPath = System.Text.ASCIIEncoding.ASCII.GetString(dbBase64Text);

Hope it helps!

Leave a Comment