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

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

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