source: trunk/ab5.0/ablib/src/Classes/System/IO/File.ab@ 668

Last change on this file since 668 was 605, checked in by イグトランス (egtra), 16 years ago

非同期入出力(Begin/End-Read/Writeメソッド)を実装。

File size: 13.1 KB
RevLine 
[271]1Namespace System
2Namespace IO
[59]3
[476]4/*
5@brief ファイルの操作,情報を取得するクラス
6@date 2008/03/13
7@author OverTaker
8*/
9
10Imports System.Collections.Generic
11
[59]12Class File
13Public
14
[466]15 '----------------------------------------------------------------
16 ' パブリック メソッド
17 '----------------------------------------------------------------
18
[476]19 /*
20 @brief ファイルにテキストを追加する
21 @param ファイルパス
22 @param 追加するテキスト
23 */
24 Static Sub AppendAllText( path As String, contents As String )
25 Dim stream = AppendText(path) As StreamWriter
26 stream.Write(contents)
27 stream.Close()
28 End Sub
[59]29
30/* Static Sub AppendAllText( path As String, contents As String, encoding As Encoding )
31 ' TODO: 実装
32 End Sub */
33
[476]34 /*
35 @brief ファイルにテキストを追加するストリームを作成する
36 @param ファイルパス
37 @return ストリームライター
38 */
39 Static Function AppendText( path As String ) As StreamWriter
40 Return New StreamWriter(Open(path, FileMode.Append))
41 End Function
[59]42
[476]43 /*
44 @brief ファイルをコピーする(上書きできない)
45 @param コピー元のファイルパス
46 @param コピー先のファイルパス
47 */
[59]48 Static Sub Copy( sourceFileName As String, destFileName As String )
[466]49 Copy(sourceFileName, destFileName, False)
[59]50 End Sub
51
[476]52 /*
53 @brief ファイルをコピーする
54 @param コピー元のファイルパス
55 @param コピー先のファイルパス
56 @param 上書きする場合True,しない場合False
57 */
[59]58 Static Sub Copy( sourceFileName As String, destFileName As String, overwrite As Boolean )
[466]59 If Not CopyFile(Path.GetFullPath(sourceFileName), Path.GetFullPath(destFileName), overwrite) Then
60 Throw New IOException( "FileInfo: Failed to CopyFile." )
61 End If
[59]62 End Sub
63
[476]64 /*
65 @brief 新しいファイルを作成し、そのストリームを取得する
66 @param ファイルパス
67 @return ファイルストリーム
68 */
[59]69 Static Function Create( path As String ) As FileStream
[466]70 Return New FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite)
[59]71 End Function
72
[466]73/* Static Function Create( path As String, bufferSize As Long ) As FileStream
[59]74 ' TODO: 実装
[466]75 End Function */
[59]76
77/* Static Function Create( path As String, bufferSize As Long, options As FileOptions ) As FileStream
78 ' TODO: 実装
79 End Function */
80
81/* Static Function Create( path As String, bufferSize As Long, options As FileOptions, fileSecurity As FileSecurity ) As FileStream
82 ' TODO: 実装
83 End Function */
84
[466]85/* Static Function CreateText( path As String ) As StreamWriter
[59]86 ' TODO: 実装
[466]87 End Function*/
[59]88
[466]89/* Static Sub Decrypt( path As String )
[59]90 ' TODO: 実装
[466]91 End Sub*/
[59]92
[476]93 /*
94 @brief ファイルを削除する
95 @param ファイルパス
96 */
[466]97 Static Sub Delete( path As String )
[476]98 If Not DeleteFile(Path.GetFullPath(path)) Then
[466]99 Throw New IOException("File.Delete: Failed to DeleteFile.")
100 End If
[59]101 End Sub
102
[466]103/* Static Sub Encrypt( path As String )
[59]104 ' TODO: 実装
[466]105 End Sub*/
[59]106
[476]107 /*
108 @brief ファイルが存在するかどうかを取得する
109 @param ファイルパス
110 @retval True 存在する
111 @retval False 存在しない
112 */
[59]113 Static Function Exists( path As String ) As Boolean
[466]114 Dim data As WIN32_FIND_DATA
[476]115 Dim hFind = FindFirstFile(ToTCStr(Path.GetFullPath(path)), data)
[466]116 FindClose(hFind)
117
118 If hFind <> INVALID_HANDLE_VALUE Then
119 Return True
120 Else
121 Return False
122 End If
[59]123 End Function
124
125/* Static Function GetAccessControl( path As String ) As FileSecurity
126 ' TODO: 実装
127 End Function */
128
129/* Static Function GetAccessControl( path As String, includeSections As AccessControlSections ) As FileSecurity
130 ' TODO: 実装
131 End Function */
132
[476]133 /*
134 @brief ファイルの属性を取得する
135 @param ファイルパス
136 @return ファイル属性
137 */
[59]138 Static Function GetAttributes( path As String ) As FileAttributes
[466]139 Return New FileAttributes(getFileData(path).dwFileAttributes As Long, "FileAttributes")
[59]140 End Function
141
[476]142 /*
143 @brief ファイルの作成日時を取得する
144 @param ファイルパス
145 @return 作成日時
146 */
[59]147 Static Function GetCreationTime( path As String ) As DateTime
[466]148 Return System.DateTime.FromFileTime(getFileData(path).ftCreationTime)
[59]149 End Function
150
[476]151 /*
152 @brief ファイルの作成日時をUTC時刻で取得する
153 @param ファイルパス
154 @return 作成日時
155 */
[59]156 Static Function GetCreationTimeUtc( path As String ) As DateTime
[466]157 Return System.DateTime.FromFileTimeUtc(getFileData(path).ftCreationTime)
[59]158 End Function
159
[476]160 /*
161 @brief ファイルの最終アクセス日時を取得する
162 @param ファイルパス
163 @return 最終アクセス日時
164 */
[59]165 Static Function GetLastAccessTime( path As String ) As DateTime
[466]166 Return System.DateTime.FromFileTime(getFileData(path).ftLastAccessTime)
[59]167 End Function
168
[476]169 /*
170 @brief ファイルの最終アクセス日時をUTC時刻で取得する
171 @param ファイルパス
172 @return 最終アクセス日時
173 */
[59]174 Static Function GetLastAccessTimeUtc( path As String ) As DateTime
[466]175 Return System.DateTime.FromFileTimeUtc(getFileData(path).ftLastAccessTime)
[59]176 End Function
177
[476]178 /*
179 @brief ファイルの最終書き込み日時を取得する
180 @param ファイルパス
181 @return 最終書き込み日時
182 */
[59]183 Static Function GetLastWriteTime( path As String ) As DateTime
[466]184 Return System.DateTime.FromFileTime(getFileData(path).ftLastWriteTime)
[59]185 End Function
186
[476]187 /*
188 @brief ファイルの最終書き込み日時をUTC時刻で取得する
189 @param ファイルパス
190 @return 最終書き込み日時
191 */
[59]192 Static Function GetLastWriteTimeUtc( path As String ) As DateTime
[466]193 Return System.DateTime.FromFileTimeUtc(getFileData(path).ftLastWriteTime)
[59]194 End Function
195
[476]196 /*
197 @brief ファイルを移動する
198 @param 移動元のファイルパス
199 @param 移動先のファイルパス
200 */
[59]201 Static Sub Move( sourceFileName As String, destFileName As String )
[466]202 If Not MoveFile(Path.GetFullPath(sourceFileName), Path.GetFullPath(destFileName)) Then
203 Throw New IOException("File.Move: Failed to MoveFile.")
204 End If
[59]205 End Sub
[60]206
[476]207 /*
208 @brief ファイルストリームを作成する
209 @param ファイルパス
210 @param ファイルモード
211 @return ファイルストリーム
212 */
[60]213 Static Function Open( path As String, mode As FileMode ) As FileStream
[466]214 Return New FileStream(path, mode)
[60]215 End Function
216
[476]217 /*
218 @brief ファイルストリームを作成する
219 @param ファイルパス
220 @param ファイルモード
221 @param ファイルアクセス
222 @return ファイルストリーム
223 */
[60]224 Static Function Open( path As String, mode As FileMode, access As FileAccess ) As FileStream
[466]225 Return New FileStream(path, mode, access)
[60]226 End Function
227
[476]228 /*
229 @brief ファイルストリームを作成する
230 @param ファイルパス
231 @param ファイルモード
232 @param ファイルアクセス
233 @param ファイル共有
234 @return ファイルストリーム
235 */
[60]236 Static Function Open( path As String, mode As FileMode, access As FileAccess, share As FileShare ) As FileStream
[466]237 Return New FileStream(path, mode, access, share)
[60]238 End Function
239
[476]240 /*
241 @brief 読み取り専用のファイルストリームを作成する
242 @param ファイルパス
243 @return ファイルストリーム
244 */
[60]245 Static Function OpenRead( path As String ) As FileStream
[466]246 Return Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)
[60]247 End Function
248
[476]249 /*
250 @brief 読み取り専用のストリームを作成する
251 @param ファイルパス
252 @return ストリームリーダー
253 */
[466]254 Static Function OpenText( path As String ) As StreamReader
255 Return New StreamReader(path)
256 End Function
[60]257
[476]258 /*
259 @brief 書き込み専用のファイルストリームを作成する
260 @param ファイルパス
261 @return ファイルストリーム
262 */
[60]263 Static Function OpenWrite( path As String ) As FileStream
[466]264 Return Open(path, FileMode.Open, FileAccess.Write)
[60]265 End Function
266
[466]267/* Static Function ReadAllBytes( path As String ) As *Byte
[60]268 ' TODO: 実装
[466]269 End Function*/
[60]270
[476]271 /*
272 @brief ファイルのすべての行を読み取る
273 @param ファイルパス
274 @return 各行の文字列が格納されたリスト
275 */
276 Static Function ReadAllLines( path As String ) As List<String>
277 Dim stream = New StreamReader(path)
278 Dim readLines As List<String>
279 Dim readLine = stream.ReadLine() As String
280 While Not ActiveBasic.IsNothing(readLine)
281 readLines.Add(readLine)
282 readLine = stream.ReadLine()
283 Wend
284 stream.Close()
285 Return readLines
286 End Function
[60]287
288/* Static Function ReadAllLines( path As String, encoding As Encoding ) As Strings
289 ' TODO: 実装
290 End Function */
291
[476]292 /*
293 @brief ファイルをすべて文字列として読み込む
294 @param ファイルパス
295 @return ファイルの内容
296 */
297 Static Function ReadAllText( path As String ) As String
298 Dim stream = OpenText(path)
299 Dim string = stream.ReadToEnd()
300 stream.Close()
301 Return string
302 End Function
[60]303
[105]304/* Static Function ReadAllText( path As String, encoding As Encoding ) As String
[60]305 ' TODO: 実装
[105]306 End Function */
[60]307
[476]308 /*
309 @brief ファイルを置き換える
310 @param 置き換えるファイルパス
311 @param 置き換えられるファイルパス
312 @param 置き換えられるファイルのバックアップを作成するファイルパス
313 */
314 Static Sub Replace( sourceFileName As String, destinationFileName As String, destinationBackupFileName As String )
315 Copy(destinationFileName, destinationBackupFileName)
316 Copy(sourceFileName, destinationFileName, True)
317 End Sub
[60]318
[466]319/* Static Sub Replace( sourceFileName As String, destinationFileName As String, destinationBackupFileName As String, ignoreMetadataErrors As Boolean )
[60]320 ' TODO: 実装
[466]321 End Sub*/
[60]322
[105]323/* Static Sub SetAccessControl( path As String, fileSecurity As FileSecurity )
[60]324 ' TODO: 実装
[105]325 End Sub */
[60]326
[476]327 /*
328 @brief ファイルの属性を設定する
329 @param ファイルパス
330 @param ファイル属性
331 */
[60]332 Static Sub SetAttributes( path As String, fileAttributes As FileAttributes )
[466]333 If Not SetFileAttributes(ToTCStr(path), fileAttributes) Then
334 Throw New IOException("File.SetAttributes: Failed to SetFileAttributes.")
335 End If
[60]336 End Sub
337
[476]338 /*
339 @brief ファイルの作成日時を設定する
340 @param ファイルパス
341 @param 作成日時
342 */
[60]343 Static Sub SetCreationTime( path As String, creationTime As DateTime )
[466]344 SetCreationTimeUtc(path, creationTime)
[60]345 End Sub
346
[476]347 /*
348 @brief ファイルの作成日時をUTC時刻で設定する
349 @param ファイルパス
350 @param 作成日時
351 */
[466]352 Static Sub SetCreationTimeUtc( path As String, creationTimeUtc As DateTime )
353 Dim hFile = createFileToSetTime(path) As HANDLE
354 SetFileTime(hFile, creationTimeUtc.ToFileTimeUtc(), ByVal 0, ByVal 0)
355 CloseHandle(hFile)
[60]356 End Sub
357
[476]358 /*
359 @brief ファイルの最終アクセス日時を設定する
360 @param ファイルパス
361 @param 最終アクセス日時
362 */
[60]363 Static Sub SetLastAccessTime( path As String, lastAccessTime As DateTime )
[466]364 SetLastAccessTimeUtc(path, lastAccessTime)
[60]365 End Sub
366
[476]367 /*
368 @brief ファイルの最終アクセス日時をUTC時刻で設定する
369 @param ファイルパス
370 @param 最終アクセス日時
371 */
[60]372 Static Sub SetLastAccessTimeUtc( path As String, lastAccessTimeUtc As DateTime )
[466]373 Dim hFile = createFileToSetTime(path) As HANDLE
374 SetFileTime(hFile, ByVal 0, lastAccessTimeUtc.ToFileTimeUtc(), ByVal 0)
375 CloseHandle(hFile)
[60]376 End Sub
377
[476]378
379 /*
380 @brief ファイルの最終書き込み日時を設定する
381 @param ファイルパス
382 @param 最終書き込み日時
383 */
[60]384 Static Sub SetLastWriteTime( path As String, lastWriteTime As DateTime )
[466]385 SetLastWriteTimeUtc(path, lastWriteTime)
[60]386 End Sub
387
[476]388 /*
389 @brief ファイルの最終書き込み日時をUTC時刻で設定する
390 @param ファイルパス
391 @param 最終書き込み日時
392 */
[60]393 Static Sub SetLastWriteTimeUtc( path As String, lastWriteTimeUtc As DateTime )
[466]394 Dim hFile = createFileToSetTime(path) As HANDLE
395 SetFileTime(hFile, ByVal 0, ByVal 0, lastWriteTimeUtc.ToFileTimeUtc())
396 CloseHandle(hFile)
[60]397 End Sub
398
[466]399/* Static Sub WriteAllBytes( path As String, bytes As *Byte )
[60]400 ' TODO: 実装
[466]401 End Sub*/
[60]402
[476]403 /*
404 @brief リストに格納された文字列を一行ずつファイルに書き込む
405 @param ファイルパス
406 @param 書き込む文字列リスト
407 */
408 Static Sub WriteAllLines( path As String, contents As List<String> )
409 Dim stream = New StreamWriter(path)
410 Dim enumerator = contents.GetEnumerator()
411 enumerator.Reset()
412 While enumerator.MoveNext()
413 stream.WriteLine(enumerator.Current)
414 Wend
415 stream.Close()
416 End Sub
[60]417
418/* Static Sub WriteAllLines( path As String, contents As Strings, encoding As Enconding )
419 ' TODO: 実装
420 End Sub */
421
[476]422 /*
423 @brief ファイルに文字列を書き込む
424 @param ファイルパス
425 @param 書き込む文字列
426 */
427 Static Sub WriteAllText( path As String, contents As String )
428 Dim stream = New StreamWriter(path)
429 stream.Write(contents)
430 stream.Close()
431 End Sub
[60]432
[105]433/* Static Sub WriteAllText( path As String, contents As String, encoding As Enconding )
[60]434 ' TODO: 実装
[105]435 End Sub */
[60]436
[466]437Private
[476]438 '----------------------------------------------------------------
439 ' プライベート メソッド
440 '----------------------------------------------------------------
[466]441
[476]442 /*
443 @brief ファイルの情報を取得する
444 @param ファイルパス
445 @return WIN32_FIND_DATA構造体
446 */
[466]447 Static Function getFileData(path As String) As WIN32_FIND_DATA
448 Dim data As WIN32_FIND_DATA
449 Dim hFind = FindFirstFile(ToTCStr(Path.GetFullPath(path)), data)
450 FindClose(hFind)
451
452 If hFind <> INVALID_HANDLE_VALUE Then
453 Return data
454 Else
455 Throw New IOException("File.getFileData: Failed to FindFirstFile.")
456 End If
457 End Function
458
[476]459 /*
460 @brief ファイルの各種日時を変更するためのファイル作成を行う
461 @param ファイルパス
462 @return ファイルハンドル
463 */
[466]464 Static Function createFileToSetTime(path As String) As HANDLE
465 Dim hFile As HANDLE
466 hFile = CreateFile(ToTCStr(path), GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
467 If hFile = INVALID_HANDLE_VALUE Then
468 CloseHandle(hFile)
469 Throw New IOException("File.setFileTime: Failed to CreateFile.")
470 Exit Function
471 End If
472 Return hFile
473 End Function
[59]474End Class
[271]475
476
477End Namespace
478End Namespace
Note: See TracBrowser for help on using the repository browser.