1 | Namespace System
|
---|
2 | Namespace IO
|
---|
3 |
|
---|
4 | /*
|
---|
5 | @brief ファイルの操作,情報を取得するクラス
|
---|
6 | @date 2008/03/13
|
---|
7 | @author OverTaker
|
---|
8 | */
|
---|
9 |
|
---|
10 | Imports System.Collections.Generic
|
---|
11 |
|
---|
12 | Class File
|
---|
13 | Public
|
---|
14 |
|
---|
15 | '----------------------------------------------------------------
|
---|
16 | ' パブリック メソッド
|
---|
17 | '----------------------------------------------------------------
|
---|
18 |
|
---|
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
|
---|
29 |
|
---|
30 | /* Static Sub AppendAllText( path As String, contents As String, encoding As Encoding )
|
---|
31 | ' TODO: 実装
|
---|
32 | End Sub */
|
---|
33 |
|
---|
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
|
---|
42 |
|
---|
43 | /*
|
---|
44 | @brief ファイルをコピーする(上書きできない)
|
---|
45 | @param コピー元のファイルパス
|
---|
46 | @param コピー先のファイルパス
|
---|
47 | */
|
---|
48 | Static Sub Copy( sourceFileName As String, destFileName As String )
|
---|
49 | Copy(sourceFileName, destFileName, False)
|
---|
50 | End Sub
|
---|
51 |
|
---|
52 | /*
|
---|
53 | @brief ファイルをコピーする
|
---|
54 | @param コピー元のファイルパス
|
---|
55 | @param コピー先のファイルパス
|
---|
56 | @param 上書きする場合True,しない場合False
|
---|
57 | */
|
---|
58 | Static Sub Copy( sourceFileName As String, destFileName As String, overwrite As Boolean )
|
---|
59 | If Not CopyFile(Path.GetFullPath(sourceFileName), Path.GetFullPath(destFileName), overwrite) Then
|
---|
60 | Throw New IOException( "FileInfo: Failed to CopyFile." )
|
---|
61 | End If
|
---|
62 | End Sub
|
---|
63 |
|
---|
64 | /*
|
---|
65 | @brief 新しいファイルを作成し、そのストリームを取得する
|
---|
66 | @param ファイルパス
|
---|
67 | @return ファイルストリーム
|
---|
68 | */
|
---|
69 | Static Function Create( path As String ) As FileStream
|
---|
70 | Return New FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite)
|
---|
71 | End Function
|
---|
72 |
|
---|
73 | /* Static Function Create( path As String, bufferSize As Long ) As FileStream
|
---|
74 | ' TODO: 実装
|
---|
75 | End Function */
|
---|
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 |
|
---|
85 | /* Static Function CreateText( path As String ) As StreamWriter
|
---|
86 | ' TODO: 実装
|
---|
87 | End Function*/
|
---|
88 |
|
---|
89 | /* Static Sub Decrypt( path As String )
|
---|
90 | ' TODO: 実装
|
---|
91 | End Sub*/
|
---|
92 |
|
---|
93 | /*
|
---|
94 | @brief ファイルを削除する
|
---|
95 | @param ファイルパス
|
---|
96 | */
|
---|
97 | Static Sub Delete( path As String )
|
---|
98 | If Not DeleteFile(Path.GetFullPath(path)) Then
|
---|
99 | Throw New IOException("File.Delete: Failed to DeleteFile.")
|
---|
100 | End If
|
---|
101 | End Sub
|
---|
102 |
|
---|
103 | /* Static Sub Encrypt( path As String )
|
---|
104 | ' TODO: 実装
|
---|
105 | End Sub*/
|
---|
106 |
|
---|
107 | /*
|
---|
108 | @brief ファイルが存在するかどうかを取得する
|
---|
109 | @param ファイルパス
|
---|
110 | @retval True 存在する
|
---|
111 | @retval False 存在しない
|
---|
112 | */
|
---|
113 | Static Function Exists( path As String ) As Boolean
|
---|
114 | Dim data As WIN32_FIND_DATA
|
---|
115 | Dim hFind = FindFirstFile(ToTCStr(Path.GetFullPath(path)), data)
|
---|
116 | FindClose(hFind)
|
---|
117 |
|
---|
118 | If hFind <> INVALID_HANDLE_VALUE Then
|
---|
119 | Return True
|
---|
120 | Else
|
---|
121 | Return False
|
---|
122 | End If
|
---|
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 |
|
---|
133 | /*
|
---|
134 | @brief ファイルの属性を取得する
|
---|
135 | @param ファイルパス
|
---|
136 | @return ファイル属性
|
---|
137 | */
|
---|
138 | Static Function GetAttributes( path As String ) As FileAttributes
|
---|
139 | Return New FileAttributes(getFileData(path).dwFileAttributes As Long, "FileAttributes")
|
---|
140 | End Function
|
---|
141 |
|
---|
142 | /*
|
---|
143 | @brief ファイルの作成日時を取得する
|
---|
144 | @param ファイルパス
|
---|
145 | @return 作成日時
|
---|
146 | */
|
---|
147 | Static Function GetCreationTime( path As String ) As DateTime
|
---|
148 | Return System.DateTime.FromFileTime(getFileData(path).ftCreationTime)
|
---|
149 | End Function
|
---|
150 |
|
---|
151 | /*
|
---|
152 | @brief ファイルの作成日時をUTC時刻で取得する
|
---|
153 | @param ファイルパス
|
---|
154 | @return 作成日時
|
---|
155 | */
|
---|
156 | Static Function GetCreationTimeUtc( path As String ) As DateTime
|
---|
157 | Return System.DateTime.FromFileTimeUtc(getFileData(path).ftCreationTime)
|
---|
158 | End Function
|
---|
159 |
|
---|
160 | /*
|
---|
161 | @brief ファイルの最終アクセス日時を取得する
|
---|
162 | @param ファイルパス
|
---|
163 | @return 最終アクセス日時
|
---|
164 | */
|
---|
165 | Static Function GetLastAccessTime( path As String ) As DateTime
|
---|
166 | Return System.DateTime.FromFileTime(getFileData(path).ftLastAccessTime)
|
---|
167 | End Function
|
---|
168 |
|
---|
169 | /*
|
---|
170 | @brief ファイルの最終アクセス日時をUTC時刻で取得する
|
---|
171 | @param ファイルパス
|
---|
172 | @return 最終アクセス日時
|
---|
173 | */
|
---|
174 | Static Function GetLastAccessTimeUtc( path As String ) As DateTime
|
---|
175 | Return System.DateTime.FromFileTimeUtc(getFileData(path).ftLastAccessTime)
|
---|
176 | End Function
|
---|
177 |
|
---|
178 | /*
|
---|
179 | @brief ファイルの最終書き込み日時を取得する
|
---|
180 | @param ファイルパス
|
---|
181 | @return 最終書き込み日時
|
---|
182 | */
|
---|
183 | Static Function GetLastWriteTime( path As String ) As DateTime
|
---|
184 | Return System.DateTime.FromFileTime(getFileData(path).ftLastWriteTime)
|
---|
185 | End Function
|
---|
186 |
|
---|
187 | /*
|
---|
188 | @brief ファイルの最終書き込み日時をUTC時刻で取得する
|
---|
189 | @param ファイルパス
|
---|
190 | @return 最終書き込み日時
|
---|
191 | */
|
---|
192 | Static Function GetLastWriteTimeUtc( path As String ) As DateTime
|
---|
193 | Return System.DateTime.FromFileTimeUtc(getFileData(path).ftLastWriteTime)
|
---|
194 | End Function
|
---|
195 |
|
---|
196 | /*
|
---|
197 | @brief ファイルを移動する
|
---|
198 | @param 移動元のファイルパス
|
---|
199 | @param 移動先のファイルパス
|
---|
200 | */
|
---|
201 | Static Sub Move( sourceFileName As String, destFileName As String )
|
---|
202 | If Not MoveFile(Path.GetFullPath(sourceFileName), Path.GetFullPath(destFileName)) Then
|
---|
203 | Throw New IOException("File.Move: Failed to MoveFile.")
|
---|
204 | End If
|
---|
205 | End Sub
|
---|
206 |
|
---|
207 | /*
|
---|
208 | @brief ファイルストリームを作成する
|
---|
209 | @param ファイルパス
|
---|
210 | @param ファイルモード
|
---|
211 | @return ファイルストリーム
|
---|
212 | */
|
---|
213 | Static Function Open( path As String, mode As FileMode ) As FileStream
|
---|
214 | Return New FileStream(path, mode)
|
---|
215 | End Function
|
---|
216 |
|
---|
217 | /*
|
---|
218 | @brief ファイルストリームを作成する
|
---|
219 | @param ファイルパス
|
---|
220 | @param ファイルモード
|
---|
221 | @param ファイルアクセス
|
---|
222 | @return ファイルストリーム
|
---|
223 | */
|
---|
224 | Static Function Open( path As String, mode As FileMode, access As FileAccess ) As FileStream
|
---|
225 | Return New FileStream(path, mode, access)
|
---|
226 | End Function
|
---|
227 |
|
---|
228 | /*
|
---|
229 | @brief ファイルストリームを作成する
|
---|
230 | @param ファイルパス
|
---|
231 | @param ファイルモード
|
---|
232 | @param ファイルアクセス
|
---|
233 | @param ファイル共有
|
---|
234 | @return ファイルストリーム
|
---|
235 | */
|
---|
236 | Static Function Open( path As String, mode As FileMode, access As FileAccess, share As FileShare ) As FileStream
|
---|
237 | Return New FileStream(path, mode, access, share)
|
---|
238 | End Function
|
---|
239 |
|
---|
240 | /*
|
---|
241 | @brief 読み取り専用のファイルストリームを作成する
|
---|
242 | @param ファイルパス
|
---|
243 | @return ファイルストリーム
|
---|
244 | */
|
---|
245 | Static Function OpenRead( path As String ) As FileStream
|
---|
246 | Return Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)
|
---|
247 | End Function
|
---|
248 |
|
---|
249 | /*
|
---|
250 | @brief 読み取り専用のストリームを作成する
|
---|
251 | @param ファイルパス
|
---|
252 | @return ストリームリーダー
|
---|
253 | */
|
---|
254 | Static Function OpenText( path As String ) As StreamReader
|
---|
255 | Return New StreamReader(path)
|
---|
256 | End Function
|
---|
257 |
|
---|
258 | /*
|
---|
259 | @brief 書き込み専用のファイルストリームを作成する
|
---|
260 | @param ファイルパス
|
---|
261 | @return ファイルストリーム
|
---|
262 | */
|
---|
263 | Static Function OpenWrite( path As String ) As FileStream
|
---|
264 | Return Open(path, FileMode.Open, FileAccess.Write)
|
---|
265 | End Function
|
---|
266 |
|
---|
267 | /* Static Function ReadAllBytes( path As String ) As *Byte
|
---|
268 | ' TODO: 実装
|
---|
269 | End Function*/
|
---|
270 |
|
---|
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
|
---|
287 |
|
---|
288 | /* Static Function ReadAllLines( path As String, encoding As Encoding ) As Strings
|
---|
289 | ' TODO: 実装
|
---|
290 | End Function */
|
---|
291 |
|
---|
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
|
---|
303 |
|
---|
304 | /* Static Function ReadAllText( path As String, encoding As Encoding ) As String
|
---|
305 | ' TODO: 実装
|
---|
306 | End Function */
|
---|
307 |
|
---|
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
|
---|
318 |
|
---|
319 | /* Static Sub Replace( sourceFileName As String, destinationFileName As String, destinationBackupFileName As String, ignoreMetadataErrors As Boolean )
|
---|
320 | ' TODO: 実装
|
---|
321 | End Sub*/
|
---|
322 |
|
---|
323 | /* Static Sub SetAccessControl( path As String, fileSecurity As FileSecurity )
|
---|
324 | ' TODO: 実装
|
---|
325 | End Sub */
|
---|
326 |
|
---|
327 | /*
|
---|
328 | @brief ファイルの属性を設定する
|
---|
329 | @param ファイルパス
|
---|
330 | @param ファイル属性
|
---|
331 | */
|
---|
332 | Static Sub SetAttributes( path As String, fileAttributes As FileAttributes )
|
---|
333 | If Not SetFileAttributes(ToTCStr(path), fileAttributes) Then
|
---|
334 | Throw New IOException("File.SetAttributes: Failed to SetFileAttributes.")
|
---|
335 | End If
|
---|
336 | End Sub
|
---|
337 |
|
---|
338 | /*
|
---|
339 | @brief ファイルの作成日時を設定する
|
---|
340 | @param ファイルパス
|
---|
341 | @param 作成日時
|
---|
342 | */
|
---|
343 | Static Sub SetCreationTime( path As String, creationTime As DateTime )
|
---|
344 | SetCreationTimeUtc(path, creationTime)
|
---|
345 | End Sub
|
---|
346 |
|
---|
347 | /*
|
---|
348 | @brief ファイルの作成日時をUTC時刻で設定する
|
---|
349 | @param ファイルパス
|
---|
350 | @param 作成日時
|
---|
351 | */
|
---|
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)
|
---|
356 | End Sub
|
---|
357 |
|
---|
358 | /*
|
---|
359 | @brief ファイルの最終アクセス日時を設定する
|
---|
360 | @param ファイルパス
|
---|
361 | @param 最終アクセス日時
|
---|
362 | */
|
---|
363 | Static Sub SetLastAccessTime( path As String, lastAccessTime As DateTime )
|
---|
364 | SetLastAccessTimeUtc(path, lastAccessTime)
|
---|
365 | End Sub
|
---|
366 |
|
---|
367 | /*
|
---|
368 | @brief ファイルの最終アクセス日時をUTC時刻で設定する
|
---|
369 | @param ファイルパス
|
---|
370 | @param 最終アクセス日時
|
---|
371 | */
|
---|
372 | Static Sub SetLastAccessTimeUtc( path As String, lastAccessTimeUtc As DateTime )
|
---|
373 | Dim hFile = createFileToSetTime(path) As HANDLE
|
---|
374 | SetFileTime(hFile, ByVal 0, lastAccessTimeUtc.ToFileTimeUtc(), ByVal 0)
|
---|
375 | CloseHandle(hFile)
|
---|
376 | End Sub
|
---|
377 |
|
---|
378 |
|
---|
379 | /*
|
---|
380 | @brief ファイルの最終書き込み日時を設定する
|
---|
381 | @param ファイルパス
|
---|
382 | @param 最終書き込み日時
|
---|
383 | */
|
---|
384 | Static Sub SetLastWriteTime( path As String, lastWriteTime As DateTime )
|
---|
385 | SetLastWriteTimeUtc(path, lastWriteTime)
|
---|
386 | End Sub
|
---|
387 |
|
---|
388 | /*
|
---|
389 | @brief ファイルの最終書き込み日時をUTC時刻で設定する
|
---|
390 | @param ファイルパス
|
---|
391 | @param 最終書き込み日時
|
---|
392 | */
|
---|
393 | Static Sub SetLastWriteTimeUtc( path As String, lastWriteTimeUtc As DateTime )
|
---|
394 | Dim hFile = createFileToSetTime(path) As HANDLE
|
---|
395 | SetFileTime(hFile, ByVal 0, ByVal 0, lastWriteTimeUtc.ToFileTimeUtc())
|
---|
396 | CloseHandle(hFile)
|
---|
397 | End Sub
|
---|
398 |
|
---|
399 | /* Static Sub WriteAllBytes( path As String, bytes As *Byte )
|
---|
400 | ' TODO: 実装
|
---|
401 | End Sub*/
|
---|
402 |
|
---|
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
|
---|
417 |
|
---|
418 | /* Static Sub WriteAllLines( path As String, contents As Strings, encoding As Enconding )
|
---|
419 | ' TODO: 実装
|
---|
420 | End Sub */
|
---|
421 |
|
---|
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
|
---|
432 |
|
---|
433 | /* Static Sub WriteAllText( path As String, contents As String, encoding As Enconding )
|
---|
434 | ' TODO: 実装
|
---|
435 | End Sub */
|
---|
436 |
|
---|
437 | Private
|
---|
438 | '----------------------------------------------------------------
|
---|
439 | ' プライベート メソッド
|
---|
440 | '----------------------------------------------------------------
|
---|
441 |
|
---|
442 | /*
|
---|
443 | @brief ファイルの情報を取得する
|
---|
444 | @param ファイルパス
|
---|
445 | @return WIN32_FIND_DATA構造体
|
---|
446 | */
|
---|
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 |
|
---|
459 | /*
|
---|
460 | @brief ファイルの各種日時を変更するためのファイル作成を行う
|
---|
461 | @param ファイルパス
|
---|
462 | @return ファイルハンドル
|
---|
463 | */
|
---|
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
|
---|
474 | End Class
|
---|
475 |
|
---|
476 |
|
---|
477 | End Namespace
|
---|
478 | End Namespace
|
---|