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

Last change on this file since 435 was 407, checked in by OverTaker, 16 years ago

System/IO/Directory.ab実装。例外処理を徐々に実装。

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