每日程炼-IOUtils 单元(5): TDirectory.TDirectory 的其他功能

  • A+
所属分类:[开发技巧]

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(); DEMO下载:IOUnit5

代码部分:
//IOUtils 单元(5): TDirectory 的其他功能
unit Unit1;

interface

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

type
TForm1 = class(TForm)
mmo1: TMemo;
btn1: TButton;
btn2: TButton;
btn3: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses IOUtils, Types, TypInfo;

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

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

procedure TForm1.btn3Click(Sender: TObject);
const
path = 'c:\temp\ABC';
begin
TDirectory.CreateDirectory(path);
TDirectory.SetAttributes(path, [TFileAttribute.faHidden]);
ShowMessage('创建并隐藏文件夹成功\');
end;

end.
\\窗体部分
object Form1: TForm1
Left = 0
Top = 0
Caption = #24858#20154#31508#35760'-http://www.yrnote.com'
ClientHeight = 290
ClientWidth = 456
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object mmo1: TMemo
Left = 0
Top = 0
Width = 457
Height = 233
ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861
Lines.Strings = (
'mmo1')
TabOrder = 0
end
object btn1: TButton
Left = 40
Top = 255
Width = 97
Height = 25
Caption = 'GetLogicalDrives'
TabOrder = 1
OnClick = btn1Click
end
object btn2: TButton
Left = 168
Top = 255
Width = 97
Height = 25
Caption = 'GetAttributes'
TabOrder = 2
OnClick = btn2Click
end
object btn3: TButton
Left = 304
Top = 255
Width = 89
Height = 25
Caption = 'CreateDirectory'
TabOrder = 3
OnClick = btn3Click
end
end

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
广告也精彩
avatar
广告也精彩

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: