Take ownership of a file or folder by the SID (security identifier) of the user or group.
1 2 3 4 5 6 7 8 9 10 11 |
Uses AclApi, AccCtrl; ... var path, sid: string; begin path:= 'C:\My_dir'; sid:= 'S-1-5-32-544'; setFileOwner(sid, path); end; |
Options
path - path to file or folder
sid - SID of the user or group
SIDs of some major user groups:
- S-1-1-0 All
- S-1-5-32-544 Administrators
- S-1-5-32-545 Users
- S-1-5-11 Verified
Result
True if the operation is successful, False if not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function setFileOwner(strsid: string; path:WideString):boolean; var sd:PSecurityDescriptor; i:integer; sid: Pointer; begin result:= false; ConvertStringSIDToSID(PWideChar(strsid), sid); if sid <> nil then begin GetMem(Sd, 1024); InitializeSecurityDescriptor(Sd,SECURITY_DESCRIPTOR_REVISION); SetSecurityDescriptorOwner(Sd, Sid, False); i:= SetNamedSecurityInfoW(pWidechar(path), SE_FILE_OBJECT,OWNER_SECURITY_INFORMATION, sid, nil, nil, nil); if i = ERROR_SUCCESS then result:= true else result:= false; FreeMem(Sd); end; end; |