How do I programmatically locate my Dropbox folder

2019-01-08 12:42发布

How do I programmatically locate my Dropbox folder using C#? * Registry? * Environment Variable? * Etc...

标签: c# dropbox
8条回答
狗以群分
2楼-- · 2019-01-08 12:53

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!

查看更多
干净又极端
3楼-- · 2019-01-08 12:54
public static string getDropBoxPath()
    {
        try
        {
            var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            var dbPath = Path.Combine(appDataPath, "Dropbox\\host.db");
            if (!File.Exists(dbPath))
            {
                return null;
            }
            else
            {
                var lines = File.ReadAllLines(dbPath);
                var dbBase64Text = Convert.FromBase64String(lines[1]);
                var folderPath = Encoding.UTF8.GetString(dbBase64Text);
                return folderPath;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
查看更多
祖国的老花朵
4楼-- · 2019-01-08 12:59

The host.db method has stopped working in later versions of dropbox.

https://www.dropbox.com/en/help/4584 gives the recommended approach.

Here is the c# code I wrote to parse the json and get the dropbox folder.

       // https://www.dropbox.com/en/help/4584 says info.json file is in one of two places
       string filename = Environment.ExpandEnvironmentVariables( @"%LOCALAPPDATA%\Dropbox\info.json" );
       if ( !File.Exists( filename ) ) filename = Environment.ExpandEnvironmentVariables( @"%APPDATA%\Dropbox\info.json" );
       JavaScriptSerializer serializer = new JavaScriptSerializer();
       // When deserializing a string without specifying a type you get a dictionary <string, object>
       Dictionary<string, object> obj = serializer.DeserializeObject( File.ReadAllText( filename ) ) as Dictionary<string, object>;
       obj = obj[ "personal" ] as Dictionary<string, object>;
       string path = obj[ "path" ] as string;
       return path;
查看更多
We Are One
5楼-- · 2019-01-08 13:00

Dropbox has added a new helper, there is a JSON file in either %APPDATA%\Dropbox\info.json or %LOCALAPPDATA%\Dropbox\info.json.

See https://www.dropbox.com/help/4584 for more information.

查看更多
地球回转人心会变
6楼-- · 2019-01-08 13:01

Cleaner version based on previous answers (use var, added exists check, remove warnings):

    private static string GetDropBoxPath()
    {
        var appDataPath = Environment.GetFolderPath(
                                           Environment.SpecialFolder.ApplicationData);
        var 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.UTF8.GetString(dbBase64Text);

        return folderPath;
    }
查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-08 13:03

UPDATE JULY 2016: THE CODE BELOW NO LONGER WORKS DUE TO CHANGES IN THE DROPBOX CLIENT, SEE ACCEPTED ANSWER ABOVE FOR UP-TO-DATE SOLUTION

Reinaldo's answer is essentially correct but it gives some junk output before the path because there seem to be two lines in the host.db file and in this case you only want to read the second one. The following will get you just the path.

string appDataPath = Environment.GetFolderPath(
                                   Environment.SpecialFolder.ApplicationData);
string dbPath = System.IO.Path.Combine(appDataPath, "Dropbox\\host.db");
string[] lines = System.IO.File.ReadAllLines(dbPath);
byte[] dbBase64Text = Convert.FromBase64String(lines[1]);
string folderPath = System.Text.ASCIIEncoding.ASCII.GetString(dbBase64Text);
Console.WriteLine(folderPath);
查看更多
登录 后发表回答