Delphi 2010 新增功能之: IOUtils 单元(5): TDirectory 的其他功能

时间:2021-07-01 12:26:00


TDirectory.CreateDirectory(); {建立新目录}
TDirectory.Exists(); {判断文件夹是否存在}
TDirectory.IsEmpty(); {判断文件夹是否为空}
TDirectory.Copy(); {复制文件夹}
TDirectory.Move(); {移动文件夹}
TDirectory.Delete(); {删除文件夹, 第二个参数为 True 可删除非空文件夹}
TDirectory.GetDirectoryRoot(); {获取目录的根盘符, 如: C:\}
TDirectory.GetCurrentDirectory; {获取当前目录}
TDirectory.SetCurrentDirectory(); {设置当前目录}
TDirectory.GetLogicalDrives; {获取驱动器列表; 下有举例}
TDirectory.GetAttributes(); {获取文件夹属性, 譬如只读、存档等; 下有举例}
TDirectory.SetAttributes(); {设置文件夹属性; 下有举例}

{获取、设置文件建立时间、修改时间、读取时间}
TDirectory.GetCreationTime();
TDirectory.SetCreationTime();
TDirectory.GetLastWriteTime();
TDirectory.SetLastWriteTime();
TDirectory.GetLastAccessTime();
TDirectory.SetLastAccessTime();

{使用 Utc 时间格式读取或设置}
TDirectory.GetCreationTimeUtc();
TDirectory.SetCreationTimeUtc();
TDirectory.GetLastWriteTimeUtc();
TDirectory.SetLastWriteTimeUtc();
TDirectory.GetLastAccessTimeUtc();
TDirectory.SetLastAccessTimeUtc();



测试代码:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses IOUtils, Types, TypInfo;

//TDirectory.GetLogicalDrives; {获取驱动器列表}
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
arr: TStringDynArray;
begin
arr := TDirectory.GetLogicalDrives;
Memo1.Clear;
for s in arr do Memo1.Lines.Add(s);
end;

//TDirectory.GetAttributes(); {获取文件夹属性, 譬如只读、存档等}
procedure TForm1.Button2Click(Sender: TObject);
var
fas: TFileAttributes;
fa: TFileAttribute;
begin
fas := TDirectory.GetAttributes('c:\Windows');
Memo1.Clear;
for fa := Low(TFileAttribute) to High(TFileAttribute) do
if fa in fas then Memo1.Lines.Add(GetEnumName(TypeInfo(TFileAttribute), Ord(fa)));
end;

//TDirectory.SetAttributes(); {设置文件夹属性, 这里是设置为隐藏}
procedure TForm1.Button3Click(Sender: TObject);
const
path = 'c:\temp\ABC';
begin
TDirectory.CreateDirectory(path);
TDirectory.SetAttributes(path, [TFileAttribute.faHidden]);
end;

end.