Rei*_*ldo 39
更新的解决方案
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(@"\\", @"\");
Run Code Online (Sandbox Code Playgroud)
如果您想解析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"];
Run Code Online (Sandbox Code Playgroud)
弃用解决方案:
您可以阅读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);
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
iva*_*tpr 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);
Run Code Online (Sandbox Code Playgroud)
Shi*_*hah 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18530 次 |
| 最近记录: |