简体   繁体   English

如何使用 C# 以编程方式定位我的 Dropbox 文件夹?

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

How do I programmatically locate my Dropbox folder using C#?如何使用 C# 以编程方式定位我的 Dropbox 文件夹? * Registry? * 注册表? * Environment Variable? * 环境变量? * Etc... * ETC...

UPDATED SOLUTION 更新的解决方案

Dropbox now provides an info.json file as stated here: https://www.dropbox.com/en/help/4584 现在,Dropbox提供了一个info.json文件,如下所示: 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: 如果您不想处理JSON解析,则可以使用以下解决方案:

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: 如果您想解析JSON,可以使用JavaScripSerializer,如下所示:

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. 您可以阅读dropbox \\ host.db文件。 It's a Base64 file located in your AppData\\Roaming path. 这是位于AppData \\ Roaming路径中的Base64文件。 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! 希望能帮助到你!

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 2016年7月更新:由于DROPBOX客户端的更改,此代码不再起作用,请参见上面的最新解答

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. Reinaldo的回答本质上是正确的,但是它在路径之前给出了一些垃圾输出,因为host.db文件中似乎有两行,在这种情况下,您只想阅读第二行。 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);

Cleaner version based on previous answers (use var, added exists check, remove warnings): 基于先前答案的更干净的版本(使用var,添加了存在检查,删除了警告):

    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;
    }

这似乎是Dropbox的建议解决方案: https ://www.dropbox.com/help/4584 ? path =desktop_client_and_web_app

Dropbox has added a new helper, there is a JSON file in either %APPDATA%\\Dropbox\\info.json or %LOCALAPPDATA%\\Dropbox\\info.json . Dropbox添加了一个新的帮助器, %APPDATA%\\Dropbox\\info.json%LOCALAPPDATA%\\Dropbox\\info.json有一个JSON文件。

See https://www.dropbox.com/help/4584 for more information. 有关更多信息,请参见https://www.dropbox.com/help/4584

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;
        }
    }

The host.db method has stopped working in later versions of dropbox. host.db方法已停止在Dropbox的更高版本中工作。

https://www.dropbox.com/en/help/4584 gives the recommended approach. https://www.dropbox.com/cn/help/4584提供了推荐的方法。

Here is the c# code I wrote to parse the json and get the dropbox folder. 这是我编写的用于解析json并获取dropbox文件夹的c#代码。

       // 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;

I'm posting here a solution that does not use Dictionary;我在这里发布了一个不使用字典的解决方案; so many years after original answers, every time that I try to use answers from Reinaldo and Derek, I get a Could not load type 'System.Web.Util.Utf16StringValidator' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=... using both LinqPad 7 (.NET 6.0.9) and VS 2022 (Net Standard 2.0),在原始答案之后这么多年,每次我尝试使用 Reinaldo 和 Derek 的答案时,我都会从程序集“System.Web, Version=4.0.0”中得到一个无法加载类型“System.Web.Util.Utf16StringValidator”。 =neutral, PublicKeyToken=...同时使用 LinqPad 7 (.NET 6.0.9) 和 VS 2022 (Net Standard 2.0),

I do not know if this error is because I'm already referencing Newtonsoft.Json in Assembly as suggested in this unaccepted answer .我不知道这个错误是否是因为我已经按照这个 unaccepted answer中的建议在 Assembly 中引用了 Newtonsoft.Json 。

Anyway, here is 2022 piece of cake way to do it:无论如何,这是 2022 年的小菜一碟:

private static string GetDropBoxPath()
{
    // https://www.dropbox.com/en/help/4584 says info.json file is in one of two places
    string jsonPath = Environment.ExpandEnvironmentVariables(@"%LOCALAPPDATA%\Dropbox\info.json");
    if (!File.Exists(jsonPath)) jsonPath = Environment.ExpandEnvironmentVariables(@"%APPDATA%\Dropbox\info.json");
    var dropbox = JsonConvert.DeserializeObject<DropboxRoot>(File.ReadAllText(jsonPath));
    return dropbox.personal.path;
}

And these are the auxiliary classes:这些是辅助类:

public class DropboxRoot
{
    public Personal personal { get; set; }
}

public class Personal
{
    public string path { get; set; }
    public long host { get; set; }
    public bool is_team { get; set; }
    public string subscription_type { get; set; }
}

It's not stored in the registry (at least it isn't in plain text). 它没有存储在注册表中(至少不是纯文本格式)。 I believe it's stored in the following location. 我相信它存储在以下位置。

C:\\Users\\userprofile\\AppData\\Roaming\\Dropbox C:\\ Users \\ userprofile \\ AppData \\ Roaming \\ Dropbox

I would say it resides in the host.db or unlink.db file. 我会说它驻留在host.db或unlink.db文件中。

The config.db is a sqlite file. config.db是一个sqlite文件。 The other two are unknown (encrypted). 其他两个未知(已加密)。 The config.db contains a blob field only with the schema version. config.db仅包含架构版本的blob字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM