TDirectory.GetAttributes、TDirectory.SetAttributes获取和设置文件夹属性

时间:2023-12-21 13:29:38

使用函数:

  System.IOUtils.TDirectory.GetAttributes//获取属性
  System.IOUtils.TDirectory.SetAttributes//设置属性

注:次例未添加异常处理。

补充一句代码:

CheckListBox1.CheckAll(vcl.StdCtrls.TCheckBoxState(0), False, False);//ListCheckBox取消所有选中

示例:

TDirectory.GetAttributes、TDirectory.SetAttributes获取和设置文件夹属性

代码:

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.TypInfo, Vcl.StdCtrls,
Vcl.CheckLst, Vcl.FileCtrl, System.IOUtils; type
TForm1 = class(TForm)
Button_ChooseFolder: TButton;
Button_GetAttrs: TButton;
Button_SetAttrs: TButton;
CheckListBox1: TCheckListBox;
Label1: TLabel;
Edit_Path: TEdit;
procedure Button_ChooseFolderClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button_GetAttrsClick(Sender: TObject);
procedure Button_SetAttrsClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} var
sDir: string; procedure TForm1.Button_ChooseFolderClick(Sender: TObject);
begin
if not SelectDirectory('请选择一个文件夹', 'H:\', sDir) then
Exit;
Edit_Path.Text := sDir;
Button_SetAttrs.Enabled := True;
Button_GetAttrs.Enabled := True;
end; procedure TForm1.Button_GetAttrsClick(Sender: TObject);
var
FileAttrs: TFileAttributes;
FileAttr: TFileAttribute;
I: Integer;
str: string;
begin
FileAttrs := TDirectory.GetAttributes(sDir);
for FileAttr := Low(TFileAttribute) to High(TFileAttribute) do
begin
if FileAttr in FileAttrs then
str := GetEnumName
(TypeInfo(TFileAttribute), Ord(FileAttr));
for I := to CheckListBox1.Count - do
begin
if CheckListBox1.Items[I] = str then
CheckListBox1.Checked[I] := True;
end; end; end; procedure TForm1.Button_SetAttrsClick(Sender: TObject);
var
FileAttrs: TFileAttributes;
FileAttr: TFileAttribute;
I: Integer;
begin
//设置属性
FileAttrs := [];
for I := to CheckListBox1.Count - do
begin
if CheckListBox1.Checked[I] then
FileAttrs := FileAttrs + [TFileAttribute(I)];
end;
TDirectory.SetAttributes(Edit_Path.Text, FileAttrs); end; procedure TForm1.FormCreate(Sender: TObject);
begin
Button_SetAttrs.Enabled := False;
Button_GetAttrs.Enabled := False;
Edit_Path.ReadOnly := True;
end; end.