source: trunk/ab5.0/ablib/src/Classes/System/IO/FileSystemInfo.ab@ 531

Last change on this file since 531 was 466, checked in by OverTaker, 16 years ago

File.ab,FileInfo.abをそれなりに実装

File size: 6.7 KB
Line 
1Namespace System
2Namespace IO
3
4
5Class FileSystemInfo
6 m_CreationTime As FILETIME
7 m_LastAccessTime As FILETIME
8 m_LastWriteTime As FILETIME
9 m_FileAttributes As FileAttributes
10
11 m_IsFreshed As Boolean
12Protected
13 FullPath As String
14 OriginalPath As String
15Public
16
17 /*!
18 @brief コンストラクタ
19 @author OverTaker
20 */
21 Sub FileSystemInfo()
22 m_IsFreshed = False
23 End Sub
24
25 /*!
26 @brief デストラクタ
27 @author OverTaker
28 */
29 Sub ~FileSystemInfo()
30 End Sub
31
32 /*!
33 @brief 値が等しいか比較する
34 @author OverTaker
35 @param 比較するオブジェクト
36 @return 等しい場合True,そうでない場合False
37 */
38 Override Function Equals( object As Object ) As Boolean
39 If This.ToString = object.ToString Then
40 Return True
41 Else
42 Return False
43 End If
44 End Function
45
46 /*!
47 @brief 文字列で表したオブジェクトを取得する
48 @author OverTaker
49 @return オブジェクトの文字列(ファイルパス)
50 */
51 Override Function ToString() As String
52 Return FullPath
53 End Function
54
55 '----------------------------------------------------------------
56 ' パブリック プロパティ
57 '----------------------------------------------------------------
58
59 /*!
60 @brief ファイルの属性を取得する
61 @author OverTaker
62 @return ファイル属性
63 */
64 Function Attributes() As FileAttributes
65 If Not m_IsFreshed Then Refresh()
66 Return m_FileAttributes
67 End Function
68
69 /*!
70 @brief ファイル属性を設定する
71 @author OverTaker
72 @param ファイル属性
73 */
74 Sub Attributes(value As FileAttributes)
75 If Not SetFileAttributes(ToTCStr(FullPath), value) Then
76 Throw New IOException("FileSystemInfo.Attributes: Failed to SetFileAttributes.")
77 End If
78 End Sub
79
80 /*!
81 @brief ファイル作成日を取得する
82 @author OverTaker
83 @return ファイルの作成日
84 */
85 Function CreationTime() As DateTime
86 If Not m_IsFreshed Then Refresh()
87 Return DateTime.FromFileTime(m_CreationTime)
88 End Function
89
90 /*!
91 @brief ファイル作成日を設定する
92 @author OverTaker
93 @param ファイルの作成日
94 */
95 Sub CreationTime(ByRef value As DateTime)
96 m_CreationTime = value.ToFileTimeUtc()
97 setFileTime()
98 End Sub
99
100 /*!
101 @brief ファイル作成日をUTC時刻で取得する
102 @author OverTaker
103 @return ファイルの作成日(UTC)
104 */
105 Function CreationTimeUtc() As DateTime
106 Return CreationTime.ToUniversalTime()
107 End Function
108
109 /*!
110 @brief ファイル作成日をUTC時刻で設定する
111 @author OverTaker
112 @param ファイルの作成日(UTC)
113 */
114 Sub CreationTimeUtc(ByRef value As DateTime)
115 CreationTime = value
116 End Sub
117
118 /*!
119 @brief ファイル最終アクセス日を取得する
120 @author OverTaker
121 @return ファイルの最終アクセス日
122 */
123 Function LastAccessTime() As DateTime
124 If Not m_IsFreshed Then Refresh()
125 Return DateTime.FromFileTime(m_LastAccessTime)
126 End Function
127
128 /*!
129 @brief ファイル最終アクセス日を設定する
130 @author OverTaker
131 @param ファイルの最終アクセス日
132 */
133 Sub LastAccessTime(ByRef value As DateTime)
134 m_LastAccessTime = value.ToFileTimeUtc()
135 setFileTime()
136 End Sub
137
138 /*!
139 @brief ファイル最終アクセス日をUTC時刻で取得する
140 @author OverTaker
141 @return ファイルの最終アクセス日(UTC)
142 */
143 Function LastAccessTimeUtc() As DateTime
144 Return LastAccessTime.ToUniversalTime()
145 End Function
146
147 /*!
148 @brief ファイル最終アクセス日をUTC時刻で設定する
149 @author OverTaker
150 @param ファイルの最終アクセス日(UTC)
151 */
152 Sub LastAccessTimeUtc(ByRef value As DateTime)
153 LastAccessTime = value
154 End Sub
155
156 /*!
157 @brief ファイルの最終書き込み日を取得する
158 @author OverTaker
159 @return ファイルの最終書き込み日
160 */
161 Function LastWriteTime() As DateTime
162 If Not m_IsFreshed Then Refresh()
163 Return DateTime.FromFileTime(m_LastWriteTime)
164 End Function
165
166 /*!
167 @brief ファイルの最終書き込み日を設定する
168 @author OverTaker
169 @param ファイルの最終書き込み日
170 */
171 Sub LastWriteTime(ByRef value As DateTime)
172 m_LastWriteTime = value.ToFileTimeUtc()
173 setFileTime()
174 End Sub
175
176 /*!
177 @brief ファイルの最終書き込み日をUTC時刻で取得する
178 @author OverTaker
179 @return ファイルの最終書き込み日(UTC)
180 */
181 Function LastWriteTimeUtc() As DateTime
182 Return LastWriteTime.ToUniversalTime()
183 End Function
184
185 /*!
186 @brief ファイルの最終書き込み日をUTC時刻で設定する
187 @author OverTaker
188 @param ファイルの最終書き込み日(UTC)
189 */
190 Sub LastWriteTimeUtc(ByRef value As DateTime)
191 LastWriteTime = value
192 End Sub
193
194 /*!
195 @brief ファイルが存在するかどうかを取得する
196 @author OverTaker
197 @return ファイルが存在する場合True,そうでない場合False
198 */
199 Function Exists() As Boolean
200 Return File.Exists(FullPath)
201 End Function
202
203 /*!
204 @brief ファイル拡張子を取得する
205 @author OverTaker
206 @return ファイル拡張子
207 */
208 Function Extension() As String
209 Return Path.GetExtension(FullPath)
210 End Function
211
212 /*!
213 @brief ファイルパスを取得する
214 @author OverTaker
215 @return ファイルパス
216 */
217 Function FullName() As String
218 Return FullPath
219 End Function
220
221 /*!
222 @brief ファイル名を取得する
223 @author OverTaker
224 @return ファイル名
225 */
226 Function Name() As String
227 Return Path.GetFileName(FullPath)
228 End Function
229
230
231 '----------------------------------------------------------------
232 ' パブリック メソッド
233 '----------------------------------------------------------------
234
235 /*!
236 @brief ファイルを削除する
237 @author OverTaker
238 */
239 Virtual Sub Delete()
240 If DeleteFile(ToTCStr(FullPath)) = FALSE Then
241 Throw New IOException("FileSystemInfo.Delete: Failed to DeleteFile.")
242 End If
243 End Sub
244
245 /*!
246 @brief ファイルを最新の情報に更新する
247 @author OverTaker
248 */
249 Virtual Sub Refresh()
250 Dim data As WIN32_FIND_DATA
251 Dim hFind = FindFirstFile(ToTCStr(FullPath), data)
252 FindClose(hFind)
253
254 If hFind <> INVALID_HANDLE_VALUE Then
255 m_FileAttributes = New FileAttributes(data.dwFileAttributes As Long, "FileAttributes")
256 m_CreationTime = data.ftCreationTime
257 m_LastAccessTime = data.ftLastAccessTime
258 m_LastWriteTime = data.ftLastWriteTime
259 m_IsFreshed = True
260 Else
261 Throw New IOException("FileSystemInfo.Refresh: Failed to FindFirstFile.")
262 End If
263 End Sub
264
265
266 '----------------------------------------------------------------
267 ' パブリック プライベート
268 '----------------------------------------------------------------
269Private
270
271 /*!
272 @brief ファイルの時間を更新する
273 @author OverTaker
274 */
275 Sub setFileTime()
276 If Not m_IsFreshed Then Refresh()
277 Dim hFile = CreateFile(ToTCStr(FullPath), GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
278 If hFile = INVALID_HANDLE_VALUE Then
279 Throw New IOException("FileSystemInfo.setFileTime: Failed to CreateFile.")
280 Exit Function
281 End If
282
283 If SetFileTime(hFile, m_CreationTime, m_LastAccessTime, m_LastWriteTime) = False Then
284 CloseHandle(hFile)
285 Throw New IOException("FileSystemInfo.setFileTime: Failed to SetFileTime.")
286 End If
287
288 CloseHandle(hFile)
289 End Sub
290End Class
291
292
293End Namespace
294End Namespace
Note: See TracBrowser for help on using the repository browser.