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
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
[462]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
[84]208 Return False
209 End If
[83]210 End Function
211
[406]212 /*!
213 @brief ファイル拡張子を取得する
214 @author OverTaker
215 @return ファイル拡張子
216 */
[83]217 Function Extension() As String
218 Return Path.GetExtension(FullPath)
219 End Function
220
[406]221 /*!
222 @brief ファイルパスを取得する
223 @author OverTaker
224 @return ファイルパス
225 */
[83]226 Function FullName() As String
227 Return FullPath
228 End Function
229
[406]230 /*!
231 @brief ファイル名を取得する
232 @author OverTaker
233 @return ファイル名
234 */
[83]235 Function Name() As String
236 Return Path.GetFileName(FullPath)
237 End Function
238
[406]239
240 '----------------------------------------------------------------
241 ' パブリック メソッド
242 '----------------------------------------------------------------
243
244 /*!
245 @brief ファイルを削除する
246 @author OverTaker
247 */
[83]248 Virtual Sub Delete()
[400]249 If DeleteFile(ToTCStr(FullPath)) = FALSE Then
[441]250 Throw New IOException("FileSystemInfo.Delete: Failed to DeleteFile.")
[83]251 End If
252 End Sub
253
[406]254 /*!
255 @brief ファイルを最新の情報に更新する
256 @author OverTaker
257 */
[130]258 Virtual Sub Refresh()
[84]259 Dim data As WIN32_FIND_DATA
[142]260 Dim hFind = FindFirstFile(ToTCStr(FullPath), data)
[130]261 FindClose(hFind)
[84]262
[407]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
[441]270 Throw New IOException("FileSystemInfo.Refresh: Failed to FindFirstFile.")
[407]271 End If
[83]272 End Sub
[406]273
274
275 '----------------------------------------------------------------
276 ' パブリック プライベート
277 '----------------------------------------------------------------
[83]278Private
[406]279
280 /*!
281 @brief ファイルの時間を更新する
282 @author OverTaker
283 */
[333]284 Sub setFileTime()
[345]285 If Not m_IsFreshed Then Refresh()
[142]286 Dim hFile = CreateFile(ToTCStr(FullPath), GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
[83]287 If hFile = INVALID_HANDLE_VALUE Then
[441]288 Throw New IOException("FileSystemInfo.setFileTime: Failed to CreateFile.")
[83]289 Exit Function
290 End If
291
[333]292 If SetFileTime(hFile, m_CreationTime, m_LastAccessTime, m_LastWriteTime) = False Then
[441]293 CloseHandle(hFile)
294 Throw New IOException("FileSystemInfo.setFileTime: Failed to SetFileTime.")
[83]295 End If
[84]296
297 CloseHandle(hFile)
[333]298 End Sub
[271]299End Class
300
301
302End Namespace
303End Namespace
Note: See TracBrowser for help on using the repository browser.