Search code examples
delphidirectorydefaultonedrive

Delphi: How to avoid duplication (OneDrive) in Document Folder setting?


Trying to store user settings & default saved data location in a less confusing way.

On PC1,

Label1.Caption := TPath.GetDocumentsPath;

shows me C:\Users\Mike\Documents, on PC2 it shows C:\users\mike\OneDrive\Documents.

On PC2 I have two sets of \Documents files & folders which do not match each other, and finding a setting or document involves a search. I have had feedback from a user in Singapore who similarly reported struggling to find their data.

As my application by default stores user settings in a subfolder of \Documents and there are users all over the world with different implementations, I would like advice.

I assume I should accept whatever is supplied by TPath.GetDocumentsPath without trying to modify it?

I could raise a warning if my application can detect two \Documents folders.

Or should I set a default store somewhere else?


Solution

  • I use Local AppData folder to store settings that depends on the user and ProgramData to store the global settings (independent of user). Code extract:

    var
        CommonPath   : array [0..MAX_PATH] of Char;
        LocalPath    : array [0..MAX_PATH] of Char;
        LangFileName : String;
    begin
        SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, SHGFP_TYPE_CURRENT, @CommonPath[0]);
        SHGetFolderPath(0, CSIDL_LOCAL_APPDATA,  0, SHGFP_TYPE_CURRENT, @LocalPath[0]);
        FAppName        := ChangeFileExt(ExtractFileName(Application.ExeName), '');
        FCommonAppData  := IncludeTrailingPathDelimiter(CommonPath) +
                           CompanyFolder + '\' + FAppName + '\';
        FLocalAppData   := IncludeTrailingPathDelimiter(LocalPath) +
                           CompanyFolder + '\' + FAppName + '\';
        ForceDirectories(FCommonAppData);
        ForceDirectories(FLocalAppData);
    end;
    

    The variables are in the protected stion in form's class:

        FLocalAppData                 : String;
        FCommonAppData                : String;
        FAppName                      : String;
    

    And CompanyName is a constant whose value is obviously my company name.