Tuesday, March 26, 2013

How to find the Dropbox directory programmatically in C#

If you need to know where the user puts the Dropbox directory you could just ask him/her and put a textbox in the settings for them to fill out. But it's bothersome and inconvenient, and the less we could ask the user the better.
Dropbox puts a file in the ApplicationData directory, called "host.db", which contains the Dropbox path in the second line. It's fairly simple to access that data, we just read the whole file (which is small), than we put the second line in a byte array converting it, and in the end we just put the string in a variable, converting in ASCII. It is simple and effective, doesn't require much time, and you can easily detect if an user has Dropbox installed or not by just checking the file existence in the default path. Here's the code:
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string dropboxPath = System.IO.Path.Combine(appData, "Dropbox\\host.db");
string[] lines = System.IO.File.ReadAllLines(dropboxPath);
byte[] dropboxBase64 = Convert.FromBase64String(lines[1]);
string folderPath = System.Text.ASCIIEncoding.ASCII.GetString(dropboxBase64);

1 comment:

  1. On the Newer DropBox I have found out the it is not in the Roaming File it in the Local File path.... Here is the code that I have got working with DropBox.

    string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
    string dropboxPath = System.IO.Path.Combine(appData, "Dropbox\\host.db");
    string[] lines = System.IO.File.ReadAllLines(dropboxPath);
    byte[] dropboxBase64 = Convert.FromBase64String(lines[1]);
    string folderPath = System.Text.ASCIIEncoding.ASCII.GetString(dropboxBase64);

    ReplyDelete

prettyprint