-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSystem.IO.FileSystem.pas
More file actions
159 lines (130 loc) · 4.31 KB
/
Copy pathSystem.IO.FileSystem.pas
File metadata and controls
159 lines (130 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
unit System.IO.FileSystem;
interface
uses
System.SysUtils, System.IOUtils, System.Classes;
type
TFileSystemInfo = class abstract
private
FFullPath: string;
FOriginalPath: string;
function GetAttributes: TFileAttributes; virtual;
protected
function GetFullPath: string; virtual;
function GetFullName: string; virtual;
function GetName: string; virtual;
function GetCreationTime: TDateTime; virtual; abstract;
function GetCreationTimeUtc: TDateTime; virtual; abstract;
function GetLastWriteTimeUtc: TDateTime; virtual; abstract;
/// <summary>
/// Represents the fully qualified path of the directory or file.
/// </summary>
property FullPath: string read GetFullPath;
/// <summary>
/// The path originally specified by the user, whether relative or absolute.
/// </summary>
property OriginalPath: string read FOriginalPath;
public
/// <summary>
/// Gets a value indicating whether the file or directory exists.
/// </summary>
function Exists: Boolean; virtual; abstract;
property Attributes: TFileAttributes read GetAttributes;
/// Gets or sets the creation time of the current file or directory.
property CreationTime: TDateTime read GetCreationTime;
/// Gets or sets the creation time, in coordinated universal time (UTC), of the current file or directory.
property CreationTimeUtc: TDateTime read GetCreationTimeUtc;
/// Gets the full path of the directory or file.
property FullName: string read GetFullName;
/// For files, gets the name of the file. For directories, gets the name of the last directory in the hierarchy if a hierarchy exists.
/// Otherwise, the Name property gets the name of the directory.
property Name: string read GetName;
/// Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.
property LastWriteTimeUtc: TDateTime read GetLastWriteTimeUtc;
end;
TDirectoryInfo = class sealed(TFileSystemInfo)
protected
function GetCreationTime: TDateTime; override;
function GetCreationTimeUtc: TDateTime; override;
function GetLastWriteTimeUtc: TDateTime; override;
public
function Exists: Boolean; override;
end;
TFileInfo = class sealed(TFileSystemInfo)
protected
function GetCreationTime: TDateTime; override;
function GetCreationTimeUtc: TDateTime; override;
function GetLastWriteTimeUtc: TDateTime; override;
public
constructor Create(fullPath: string);
function Exists: Boolean; override;
//Gets the size, in bytes, of the current file.
function Length: Int64;
end;
implementation
{ TFileSystemInfo }
function TFileSystemInfo.GetAttributes: TFileAttributes;
begin
Result := TPath.GetAttributes(FullPath);
end;
function TFileSystemInfo.GetFullName: string;
begin
Result := TPath.GetFullPath(FullPath);
end;
function TFileSystemInfo.GetFullPath: string;
begin
Result := FFullPath;
end;
function TFileSystemInfo.GetName: string;
begin
Result := TPath.GetFileName(FullPath);
end;
{ TDirectoryInfo }
function TDirectoryInfo.Exists: Boolean;
begin
Result := TDirectory.Exists(FullPath);
end;
function TDirectoryInfo.GetCreationTime: TDateTime;
begin
Result := TDirectory.GetCreationTime(FullPath);
end;
function TDirectoryInfo.GetCreationTimeUtc: TDateTime;
begin
Result := TDirectory.GetCreationTimeUtc(FullPath);
end;
function TDirectoryInfo.GetLastWriteTimeUtc: TDateTime;
begin
Result := TDirectory.GetLastWriteTimeUtc(FullPath);
end;
{ TFileInfo }
constructor TFileInfo.Create(fullPath: string);
begin
FFullPath := fullPath;
end;
function TFileInfo.Exists: Boolean;
begin
Result := TFile.Exists(FullPath);
end;
function TFileInfo.GetCreationTime: TDateTime;
begin
Result := TFile.GetCreationTime(FullPath);
end;
function TFileInfo.GetCreationTimeUtc: TDateTime;
begin
Result := TFile.GetCreationTime(FullPath);
end;
function TFileInfo.GetLastWriteTimeUtc: TDateTime;
begin
Result := TFile.GetLastWriteTimeUtc(FullPath);
end;
function TFileInfo.Length: Int64;
var
Reader: TFileStream;
begin
Reader := TFile.OpenRead(FullPath);
try
Result := Reader.Size;
finally
FreeAndNil(Reader);
end;
end;
end.