System.IOUtilsユニットのTFile.SetAttributes関数を使うとファイルの属性を簡単に設定できます。
System.IOUtilsユニットはDelphi 2010以降で使用できます。
使用するユニット
uses System.IOUtils;
ファイルの属性を設定する
ファイルの属性を設定するには、TFile.SetAttributes関数を使用します。
引数のTFileAttributes型はTFileAttributeの列挙型です。
var
Path: string;
Attr: TFileAttributes;
begin
// 属性を設定するファイルのパス
Path := 'C:\sample\file.txt';
// ファイルに適用する新しい属性セット
Attr := [TFileAttribute.faReadOnly, TFileAttribute.faHidden];
// ファイル属性を設定する
TFile.SetAttributes(Path, Attr);
end;
設定できる属性はTFileAttributeをご覧ください。