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;
ParseJsonStrcould be an empty string. In this case thehost.dbfile should be checked, which currently is not the case because of theif-elsestructure. So instead of theif-elsestructure at the end of theinfo.jsoncheck, the result should be evaluated. Only in the case (and only then) it is empty, the second check of thehost-dbfile should be executed. What do you think? \$\endgroup\$exceptsection, there should be noEXITbut instead it should continue to thehost.dbcheck. What do you think? \$\endgroup\$JclSysInfo.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). \$\endgroup\$dropbox registry keys\$\endgroup\$