source: trunk/Include/Classes/System/IO/FileSystemInfo.ab@ 462

Last change on this file since 462 was 462, checked in by dai, 16 years ago

FileSystemInfo.Existsメソッドを、ファイルが存在しない場合に呼び出すと期待値Falseではなく、例外が投げられてしまう不具合を修正。

File size: 6.8 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 SetFileAttributes(ToTCStr(FullPath), value) = FALSE Then
76 'Exception
77 Debug
78 End If
79 End Sub
80
81 /*!
82 @brief ファイル作成日を取得する
83 @author OverTaker
84 @return ファイルの作成日
85 */
86 Function CreationTime() As DateTime
87 If Not m_IsFreshed Then Refresh()
88 Return DateTime.FromFileTime(m_CreationTime)
89 End Function
90
91 /*!
92 @brief ファイル作成日を設定する
93 @author OverTaker
94 @param ファイルの作成日
95 */
96 Sub CreationTime(ByRef value As DateTime)
97 m_CreationTime = value.ToFileTimeUtc()
98 setFileTime()
99 End Sub
100
101 /*!
102 @brief ファイル作成日をUTC時刻で取得する
103 @author OverTaker
104 @return ファイルの作成日(UTC)
105 */
106 Function CreationTimeUtc() As DateTime
107 Return CreationTime.ToUniversalTime()
108 End Function
109
110 /*!
111 @brief ファイル作成日をUTC時刻で設定する
112 @author OverTaker
113 @param ファイルの作成日(UTC)
114 */
115 Sub CreationTimeUtc(ByRef value As DateTime)
116 CreationTime = value
117 End Sub
118
119 /*!
120 @brief ファイル最終アクセス日を取得する
121 @author OverTaker
122 @return ファイルの最終アクセス日
123 */
124 Function LastAccessTime() As DateTime
125 If Not m_IsFreshed Then Refresh()
126 Return DateTime.FromFileTime(m_LastAccessTime)
127 End Function
128
129 /*!
130 @brief ファイル最終アクセス日を設定する
131 @author OverTaker
132 @param ファイルの最終アクセス日
133 */
134 Sub LastAccessTime(ByRef value As DateTime)
135 m_LastAccessTime = value.ToFileTimeUtc()
136 setFileTime()
137 End Sub
138
139 /*!
140 @brief ファイル最終アクセス日をUTC時刻で取得する
141 @author OverTaker
142 @return ファイルの最終アクセス日(UTC)
143 */
144 Function LastAccessTimeUtc() As DateTime
145 Return LastAccessTime.ToUniversalTime()
146 End Function
147
148 /*!
149 @brief ファイル最終アクセス日をUTC時刻で設定する
150 @author OverTaker
151 @param ファイルの最終アクセス日(UTC)
152 */
153 Sub LastAccessTimeUtc(ByRef value As DateTime)
154 LastAccessTime = value
155 End Sub
156
157 /*!
158 @brief ファイルの最終書き込み日を取得する
159 @author OverTaker
160 @return ファイルの最終書き込み日
161 */
162 Function LastWriteTime() As DateTime
163 If Not m_IsFreshed Then Refresh()
164 Return DateTime.FromFileTime(m_LastWriteTime)
165 End Function
166
167 /*!
168 @brief ファイルの最終書き込み日を設定する
169 @author OverTaker
170 @param ファイルの最終書き込み日
171 */
172 Sub LastWriteTime(ByRef value As DateTime)
173 m_LastWriteTime = value.ToFileTimeUtc()
174 setFileTime()
175 End Sub
176
177 /*!
178 @brief ファイルの最終書き込み日をUTC時刻で取得する
179 @author OverTaker
180 @return ファイルの最終書き込み日(UTC)
181 */
182 Function LastWriteTimeUtc() As DateTime
183 Return LastWriteTime.ToUniversalTime()
184 End Function
185
186 /*!
187 @brief ファイルの最終書き込み日をUTC時刻で設定する
188 @author OverTaker
189 @param ファイルの最終書き込み日(UTC)
190 */
191 Sub LastWriteTimeUtc(ByRef value As DateTime)
192 LastWriteTime = value
193 End Sub
194
195 /*!
196 @brief ファイルが存在するかどうかを取得する
197 @author OverTaker
198 @return ファイルが存在する場合True,そうでない場合False
199 */
200 Function Exists() As Boolean
201 Dim data As WIN32_FIND_DATA
202 Dim hFind = FindFirstFile(ToTCStr(FullPath), data)
203 FindClose(hFind)
204
205 If hFind <> INVALID_HANDLE_VALUE Then
206 Return True
207 Else
208 Return False
209 End If
210 End Function
211
212 /*!
213 @brief ファイル拡張子を取得する
214 @author OverTaker
215 @return ファイル拡張子
216 */
217 Function Extension() As String
218 Return Path.GetExtension(FullPath)
219 End Function
220
221 /*!
222 @brief ファイルパスを取得する
223 @author OverTaker
224 @return ファイルパス
225 */
226 Function FullName() As String
227 Return FullPath
228 End Function
229
230 /*!
231 @brief ファイル名を取得する
232 @author OverTaker
233 @return ファイル名
234 */
235 Function Name() As String
236 Return Path.GetFileName(FullPath)
237 End Function
238
239
240 '----------------------------------------------------------------
241 ' パブリック メソッド
242 '----------------------------------------------------------------
243
244 /*!
245 @brief ファイルを削除する
246 @author OverTaker
247 */
248 Virtual Sub Delete()
249 If DeleteFile(ToTCStr(FullPath)) = FALSE Then
250 Throw New IOException("FileSystemInfo.Delete: Failed to DeleteFile.")
251 End If
252 End Sub
253
254 /*!
255 @brief ファイルを最新の情報に更新する
256 @author OverTaker
257 */
258 Virtual Sub Refresh()
259 Dim data As WIN32_FIND_DATA
260 Dim hFind = FindFirstFile(ToTCStr(FullPath), data)
261 FindClose(hFind)
262
263 If hFind <> INVALID_HANDLE_VALUE Then
264 m_FileAttributes = New FileAttributes(data.dwFileAttributes As Long, "FileAttributes")
265 m_CreationTime = data.ftCreationTime
266 m_LastAccessTime = data.ftLastAccessTime
267 m_LastWriteTime = data.ftLastWriteTime
268 m_IsFreshed = True
269 Else
270 Throw New IOException("FileSystemInfo.Refresh: Failed to FindFirstFile.")
271 End If
272 End Sub
273
274
275 '----------------------------------------------------------------
276 ' パブリック プライベート
277 '----------------------------------------------------------------
278Private
279
280 /*!
281 @brief ファイルの時間を更新する
282 @author OverTaker
283 */
284 Sub setFileTime()
285 If Not m_IsFreshed Then Refresh()
286 Dim hFile = CreateFile(ToTCStr(FullPath), GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
287 If hFile = INVALID_HANDLE_VALUE Then
288 Throw New IOException("FileSystemInfo.setFileTime: Failed to CreateFile.")
289 Exit Function
290 End If
291
292 If SetFileTime(hFile, m_CreationTime, m_LastAccessTime, m_LastWriteTime) = False Then
293 CloseHandle(hFile)
294 Throw New IOException("FileSystemInfo.setFileTime: Failed to SetFileTime.")
295 End If
296
297 CloseHandle(hFile)
298 End Sub
299End Class
300
301
302End Namespace
303End Namespace
Note: See TracBrowser for help on using the repository browser.