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

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

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