zoukankan      html  css  js  c++  java
  • TDirectory.GetAttributes、TDirectory.SetAttributes获取和设置文件夹属性

    使用函数:

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

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

    补充一句代码:

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

    示例:

    代码:

    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 := 0 to CheckListBox1.Count - 1 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 := 0 to CheckListBox1.Count - 1 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.
  • 相关阅读:
    技术分享的一些好的建议
    项目经理排期的几个tip
    公司的目标和你的目标的关系
    Android实现双击事件的两种方式
    互联网公司团队建设的几个要点
    一对一还是一对多? MVP设计前提
    互联网产品研发的典型流程
    架构和模式的区别:三层架构和MVC在应用开发中的位置
    Android Studio插件:PlantUML
    Android Studio插件:GsonFromat
  • 原文地址:https://www.cnblogs.com/cause/p/3496342.html
Copyright © 2011-2022 走看看