After a copy of a file I would like to change the owner of the copied file to the owner of the original file. I've tried many (many) things but I'm getting "access denied" as LastErrorStr.
When I start my application As Administrator then I get "This security ID may not be assigned as the owner of this object."
Any idea what is wrong with my Function.
Code: Select all
Function CopyFileOwner(Const Source: String; Const Destination: String; Var ErrorStr: String): Boolean;
Var
SourceSO: ISecurityObject;
DestSO: ISecurityObject;
tmpSource: String;
tmpDestination: String;
tmpSrcExists: Boolean;
tmpDstExists: Boolean;
Begin
// Init Var
Result := False;
ErrorStr := '';
// Check Source
tmpSource := Trim(Source);
tmpDestination := Trim(Destination);
tmpSrcExists := FileExists(tmpSource);
tmpDstExists := FileExists(tmpDestination);
// Check Exists
If tmpSrcExists And tmpDstExists Then
Begin
Try
SourceSO := FileSecurity(tmpSource);
DestSO := FileSecurity(tmpDestination);
DestSO.Owner := SourceSO.Owner;
// Set Result
ErrorStr := DestSO.LastErrorStr;
Result := ErrorStr = '';
Except
On e: exception Do
ErrorStr := Trim(e.Message);
End;
End
Else
Begin
If (Not tmpSrcExists) Or (Not tmpDstExists) Then
ErrorStr := 'Error File Does Not Exist ';
If Not(tmpSrcExists) Then
ErrorStr := ErrorStr + '[' + tmpSource + ']';
If Not(tmpDstExists) Then
ErrorStr := ErrorStr + '[' + tmpDestination + ']';
End;
End;
Pascal