Hier mal die Pfade, die wir benutzen:
Delphi-Quellcode:
var
CompanyDataPath :
string;
// Pfad für Programmdateien FEST auf company z.B. C:\ProgramData\CompanyName\
ProgramDataPath :
string;
// Pfad für Programmdateien z.B. C:\ProgramData\CompanyName\ProgramName\
AppDataRoamingPath :
string;
// Pfad für Programmdateien-Benutzer (Netzwerk) z.B. C:\Users\Benutzer\AppData\Roaming\CompanyName\ProgramName\
AppDataLocalPath :
string;
// Pfad für Programmdateien-Benutzer (lokaler PC) z.B. C:\Users\Benutzer\AppData\Local\CompanyName\ProgramName\
DocumentsPath :
string;
// Pfad für Benutzerdateien z.B. C:\Users\Benutzer\Documents\CompanyName\ProgramName\
TempPath :
string;
// Pfad für Temp-Ordner z.B. C:\Users\Benutzer\AppData\Local\Temp\
const
// https://docs.microsoft.com/de-de/windows/win32/shell/knownfolderid
FOLDERID_ProgramData : TGUID = '
{62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}';
FOLDERID_RoamingAppData : TGUID = '
{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}';
FOLDERID_LocalAppData : TGUID = '
{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}';
FOLDERID_Documents : TGUID = '
{FDD39AD0-238F-46AF-ADB4-6C85480369C7}';
und dazu die Initialisierung
Delphi-Quellcode:
function GetFolderPath( folderId: TGUID): string;
var
path: LPWSTR;
begin
if SHGetKnownFolderPath( folderId, KF_FLAG_DEFAULT, 0, path) = S_OK then
begin
result := path;
if copy(result, length(result), 1) <> '\' then
result := result + '\';
end else
result := '';
end;
procedure InitPaths( app: string);
begin
CompanyDataPath := GetFolderPath( FOLDERID_ProgramData) + company + '\';
ProgramDataPath := GetFolderPath( FOLDERID_ProgramData) + company + '\' + app + '\';
AppDataRoamingPath := GetFolderPath( FOLDERID_RoamingAppData) + company + '\' + app + '\';
AppDataLocalPath := GetFolderPath( FOLDERID_LocalAppData) + company + '\' + app + '\';
DocumentsPath := GetFolderPath( FOLDERID_Documents) + company + '\' + app + '\';
TempPath := TPath.GetTempPath;
end;
und noch ein paar Hilfsroutinen
Delphi-Quellcode:
function IsDirectoryWriteable(
const Directory:
string): Boolean;
var
FileName:
String;
H: THandle;
begin
FileName := IncludeTrailingPathDelimiter(Directory) + '
_check_write.tmp';
H := CreateFile(PChar(FileName), GENERIC_READ
or GENERIC_WRITE, 0,
nil, CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY
or FILE_FLAG_DELETE_ON_CLOSE, 0);
Result := H <> INVALID_HANDLE_VALUE;
if Result
then
CloseHandle(H);
end;
function IsFileReadOnly(
const Filename:
string): boolean;
begin
if FileExists(Filename)
then begin
result := ((GetFileAttributes( PChar(FileName))
and FILE_ATTRIBUTE_READONLY) = FILE_ATTRIBUTE_READONLY);
if not result
then
result :=
not IsDirectoryWriteable( ExtractFilePath( Filename));
end else begin
result := false;
// damit keine Compilerwarung kommt
Exception.Create('
File: '+Filename+'
does not exists.');
end;
end;