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