I've put together a function in Delphi XE7 which returns the Dropbox directory path on my machine, as this path can be customized by the user.
This function currently works on my computer, but it could have many defects preventing it to work for other users on different machines. So I would be very grateful if anybody could point to these defects.
uses
System.IOUtils, System.JSON, System.NetEncoding, JclSysInfo, JclStrings
function GetDropboxPath: string;
var
tempstr: string;
function ParseJsonStr(jStr, jPar1, jPar2: string): string;
var
jObj: TJSONObject;
jVal: TJSONValue;
begin
Result:= '';
jObj := TJSONObject.ParseJSONValue(jStr) as TJSONObject;
try
jVal:= jObj.Get(jPar1).JsonValue;
Result:= TJSONObject(jVal).Get(jPar2).JsonValue.Value;
finally
jObj.Free;
end;
end;
begin
Result := '';
tempstr := IncludeTrailingBackslash(JclSysInfo.GetAppdataFolder) + 'Dropbox\';
if DirectoryExists(tempstr) then
begin
if FileExists(tempstr + 'info.json') then // try with info.json
begin
tempstr := tempstr + 'info.json';
tempstr := System.IOUtils.TFile.ReadAllText(tempstr);
try
tempstr := ParseJsonStr(tempstr, 'personal', 'path');
except
// Todo: log this error
EXIT;
end;
Result := Trim(tempstr);
end
else if FileExists(tempstr + 'host.db') then // else try with host.db
begin
tempstr := tempstr + 'host.db';
tempstr := Trim(JclStrings.StrTrimCharLeft(System.IOUtils.TFile.ReadAllText(tempstr), '0'));
Result := Trim(TNetEncoding.Base64.Decode(tempstr)); // needs XE7
end;
end;
end;
ParseJsonStr
could be an empty string. In this case thehost.db
file should be checked, which currently is not the case because of theif-else
structure. So instead of theif-else
structure at the end of theinfo.json
check, the result should be evaluated. Only in the case (and only then) it is empty, the second check of thehost-db
file should be executed. What do you think? – user1580348 Feb 23 at 15:49except
section, there should be noEXIT
but instead it should continue to thehost.db
check. What do you think? – user1580348 Feb 23 at 15:57JclSysInfo.GetAppdataFolder
(and, even if it works for DropBox, because they hard code some stuff, instead of following MS rules & allowing a choice, it is not guaranteed to work for all (read "properly coded") apps). – Mawg Apr 13 at 14:19dropbox registry keys
– Mawg Apr 15 at 12:38