2012-03-12 29 views
30

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

+0

对不起 - 我的评论是无稽之谈,没有注意到,regkeys指向的Dropbox DLL,而不是保管箱的位置。发现这个论坛帖子,但看起来像它可能无法正常工作:http://forums.dropbox.com/topic.php?id=47895我个人做与DankDank相同,但会猜测这不会对用户个人如果他们更改默认位置,则进行安装。 – 2012-03-12 00:38:44

+0

您是否在安装过程中尝试使用[Process Explorer](http://technet.microsoft.com/zh-cn/sysinternals/bb896653)以查看它的作用以及您可以找到哪些提示来检测它? – 2012-03-12 00:39:16

回答

34

更新的解决方案

如说这里

的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"]; 

DEPRECATED SOLUTION:

您可以阅读保管箱\ 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); 

希望它有帮助!

+5

使用'System.Text.Encoding.UTF8.GetString(dbBase64Text)'。我有一个用户拥有路径'C:\ Users \Jörg\ Dropbox',如果使用ASCII,将无法正确读取。 – jimbojones 2013-12-11 16:25:00

+1

任何人都使用此代码:如果您在路径之前得到垃圾数据,请查看下面的[ivanatpr答案](http://stackoverflow.com/a/10401659/348336)。 – Gildor 2015-05-26 04:35:21

+0

此方法在更高版本的Dropbox中停止工作。我的答案显示了目前的做法。 – Derek 2016-06-19 00:57:53

0

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

C:\用户\用户配置文件\应用程序数据\漫游\ Dropbox的

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

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

37

更新2016年7月:下面的代码将不再有效因改变而在Dropbox的客户端,SEE接受的答案上面UP-TO-DATE SOLUTION

雷纳尔多的答案基本上是正确的,但它提供了一些垃圾输出在路径之前,因为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

此方法似乎不再适用于最新版本的Dropbox。请参阅已接受的答案以获取更新的解决方案。 – Reinaldo 2016-06-30 13:57:28

+0

好的谢谢你的抬头,编辑答案添加一个免责声明,它已经过时 – ivanatpr 2016-07-06 03:04:00

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

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

+1

这应该是被接受的答案:) – 2015-07-15 09:34:51

+0

是的。这应该是被接受的答案。 'host.db'文件不是由更新版本的Dropbox创建的。读取'hosts.db'的旧方法可能适用于您的机器,因为没有Dropbox更新困扰删除文件。 – 2015-12-18 14:45:01

3

的Dropbox已添加一个新的帮手,在%APPDATA%\Dropbox\info.json%LOCALAPPDATA%\Dropbox\info.json有一个JSON文件。

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

+0

应该已经放入示例代码来阅读本文,并且您可能会获得更多upvotes并被接受为答案。 – DermFrench 2015-09-28 13:02:45

0
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; 
     } 
    } 
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; 
相关问题