Setting File Attributes on Delphi

Function : FileSetAttr(const FileName:String; Attr:Integer):Integer

In the Delphi program, to change the attributes of the file, we can use the function FileSetAttr(const FileName:String; Attr:Integer):Integer

Example:
Create a Form with Edit, 2 Button, 1 OpenDialog, and 2 Check Box (as shown below), write the following program listing on the Button1 at onclick event and button2 at onclick event 

procedure TF_MerubahAtributFile.Button1Click(Sender: TObject);
var mAtribut:Integer;
begin
 if OpenDialog1.Execute then begin
  Edit1.Text:=OpenDialog1.FileName;
  mAtribut:=FileGetAttr(Edit1.Text);
  if mAtribut > 0 then begin
   if (mAtribut and faReadOnly)>0 then
    CheckBoxRead.Checked:=True
    else CheckBoxRead.Checked:=False;
   if (mAtribut and faHidden)>0 then
   CheckBoxHidden.Checked:=True
   else CheckBoxHidden.Checked:=False;
  end else begin
   CheckBoxRead.Checked:=False;
   CheckBoxHidden.Checked:=False;
  end;
 end;
end;

procedure TF_MerubahAtributFile.Button2Click(Sender: TObject);
var mAtrSet:Integer;
begin
 if CheckBoxRead.Checked=True then begin
  if CheckBoxHidden.Checked=True
  then mAtrSet:=faReadOnly or faHidden
  else mAtrSet:=faReadOnly;
 end else begin
  if CheckBoxHidden.Checked=True
  then mAtrSet:=faHidden
  else mAtrSet:=0;
 end;
 if FileSetAttr(Edit1.Text,mAtrSet)=0
  then showmessage('Successfully set the attribute')
  else showmessage('Failed to set attribute');
end;

No comments:

Post a Comment