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

时间:2012-03-12 00:18:00

标签: c# dropbox

如何使用C#以编程方式找到我的Dropbox文件夹? *注册表? *环境变量? *等......

8 个答案:

答案 0 :(得分:38)

2016年7月更新:由于DROPBOX客户端的更改,以下代码不再工作,请求接受上述解决方案的答案

Reinaldo的答案基本上是正确的,但它在路径之前提供了一些垃圾输出,因为在host.db文件中似乎有两行,在这种情况下你只想阅读第二行。以下内容将为您提供道路。

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

答案 1 :(得分:37)

更新的解决方案

Dropbox现在提供了一个info.json文件,如下所述:https://www.dropbox.com/en/help/4584

如果您不想处理解析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(@"\\", @"\");

如果您想解析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"];

弃用的解决方案:

您可以阅读dropbox \ host.db文件。它是位于AppData \ Roaming路径中的Base64文件。使用此:

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

希望它有所帮助!

答案 2 :(得分:12)

基于先前答案的清洁版(使用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;
    }

答案 3 :(得分:9)

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

答案 4 :(得分:3)

Dropbox添加了一个新助手,%APPDATA%\Dropbox\info.json%LOCALAPPDATA%\Dropbox\info.json中有一个JSON文件。

有关详细信息,请参阅https://www.dropbox.com/help/4584

答案 5 :(得分:1)

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

答案 6 :(得分:0)

它没有存储在注册表中(至少它不是纯文本格式)。我相信它存储在以下位置。

C:\用户\ USERPROFILE \应用程序数据\漫游\收存箱

我会说它驻留在host.db或unlink.db文件中。

config.db是一个sqlite文件。另外两个是未知的(加密的)。 config.db仅包含模式版本的blob字段。

答案 7 :(得分:0)

host.db方法已停止在更高版本的dropbox中工作。

https://www.dropbox.com/en/help/4584提供了推荐的方法。

这是我编写的用于解析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;
相关问题