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

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

File.ab,FileInfo.abをそれなりに実装

File size: 9.3 KB
Line 
1Namespace System
2Namespace IO
3
4
5Enum FileAccess
6 Read = GENERIC_READ
7 ReadWrite = GENERIC_READ Or GENERIC_WRITE
8 Write = GENERIC_WRITE
9End Enum
10
11Enum FileAttributes
12 Archive = FILE_ATTRIBUTE_ARCHIVE
13 Compressed = FILE_ATTRIBUTE_COMPRESSED
14 Device = FILE_ATTRIBUTE_DEVICE
15 Directory = FILE_ATTRIBUTE_DIRECTORY
16 Encrypted = FILE_ATTRIBUTE_ENCRYPTED
17 Hidden = FILE_ATTRIBUTE_HIDDEN
18 Normal = FILE_ATTRIBUTE_NORMAL
19 NotContentIndexed = FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
20 Offline = FILE_ATTRIBUTE_OFFLINE
21 ReadOnly = FILE_ATTRIBUTE_READONLY
22 ReparsePoint = FILE_ATTRIBUTE_REPARSE_POINT
23 SparseFile = FILE_ATTRIBUTE_SPARSE_FILE
24 System = FILE_ATTRIBUTE_SYSTEM
25 Temporary = FILE_ATTRIBUTE_TEMPORARY
26End Enum
27
28Enum FileMode
29 Append = OPEN_ALWAYS
30 Create = CREATE_ALWAYS
31 CreateNew = CREATE_NEW
32 Open = OPEN_EXISTING
33 OpenOrCreate = OPEN_ALWAYS
34 Truncate = TRUNCATE_EXISTING
35End Enum
36
37Enum FileShare
38 None = 0
39 Read = FILE_SHARE_READ
40 Write = FILE_SHARE_WRITE
41 ReadWrite = FILE_SHARE_READ Or FILE_SHARE_WRITE
42 DeleteFile = FILE_SHARE_DELETE
43End Enum
44
45Class File
46Public
47
48 '----------------------------------------------------------------
49 ' パブリック メソッド
50 '----------------------------------------------------------------
51
52/* Static Sub AppendAllText( path As String, contents As String )
53 ' TODO: 実装
54 End Sub*/
55
56/* Static Sub AppendAllText( path As String, contents As String, encoding As Encoding )
57 ' TODO: 実装
58 End Sub */
59
60/* Static Function AppendText( path As String ) As StreamWriter
61 ' TODO: 実装
62 End Function*/
63
64 Static Sub Copy( sourceFileName As String, destFileName As String )
65 Copy(sourceFileName, destFileName, False)
66 End Sub
67
68 Static Sub Copy( sourceFileName As String, destFileName As String, overwrite As Boolean )
69 If Not CopyFile(Path.GetFullPath(sourceFileName), Path.GetFullPath(destFileName), overwrite) Then
70 Throw New IOException( "FileInfo: Failed to CopyFile." )
71 End If
72 End Sub
73
74 Static Function Create( path As String ) As FileStream
75 Return New FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite)
76 End Function
77
78/* Static Function Create( path As String, bufferSize As Long ) As FileStream
79 ' TODO: 実装
80 End Function */
81
82/* Static Function Create( path As String, bufferSize As Long, options As FileOptions ) As FileStream
83 ' TODO: 実装
84 End Function */
85
86/* Static Function Create( path As String, bufferSize As Long, options As FileOptions, fileSecurity As FileSecurity ) As FileStream
87 ' TODO: 実装
88 End Function */
89
90/* Static Function CreateText( path As String ) As StreamWriter
91 ' TODO: 実装
92 End Function*/
93
94/* Static Sub Decrypt( path As String )
95 ' TODO: 実装
96 End Sub*/
97
98 Static Sub Delete( path As String )
99 If Not DeleteFile(path) Then
100 Throw New IOException("File.Delete: Failed to DeleteFile.")
101 End If
102 End Sub
103
104/* Static Sub Encrypt( path As String )
105 ' TODO: 実装
106 End Sub*/
107
108 Static Function Exists( path As String ) As Boolean
109 Dim data As WIN32_FIND_DATA
110 Dim hFind = FindFirstFile(ToTCStr(path), data)
111 FindClose(hFind)
112
113 If hFind <> INVALID_HANDLE_VALUE Then
114 Return True
115 Else
116 Return False
117 End If
118 End Function
119
120/* Static Function GetAccessControl( path As String ) As FileSecurity
121 ' TODO: 実装
122 End Function */
123
124/* Static Function GetAccessControl( path As String, includeSections As AccessControlSections ) As FileSecurity
125 ' TODO: 実装
126 End Function */
127
128 Static Function GetAttributes( path As String ) As FileAttributes
129 Return New FileAttributes(getFileData(path).dwFileAttributes As Long, "FileAttributes")
130 End Function
131
132 Static Function GetCreationTime( path As String ) As DateTime
133 Return System.DateTime.FromFileTime(getFileData(path).ftCreationTime)
134 End Function
135
136 Static Function GetCreationTimeUtc( path As String ) As DateTime
137 Return System.DateTime.FromFileTimeUtc(getFileData(path).ftCreationTime)
138 End Function
139
140 Static Function GetLastAccessTime( path As String ) As DateTime
141 Return System.DateTime.FromFileTime(getFileData(path).ftLastAccessTime)
142 End Function
143
144 Static Function GetLastAccessTimeUtc( path As String ) As DateTime
145 Return System.DateTime.FromFileTimeUtc(getFileData(path).ftLastAccessTime)
146 End Function
147
148 Static Function GetLastWriteTime( path As String ) As DateTime
149 Return System.DateTime.FromFileTime(getFileData(path).ftLastWriteTime)
150 End Function
151
152 Static Function GetLastWriteTimeUtc( path As String ) As DateTime
153 Return System.DateTime.FromFileTimeUtc(getFileData(path).ftLastWriteTime)
154 End Function
155
156 Static Sub Move( sourceFileName As String, destFileName As String )
157 If Not MoveFile(Path.GetFullPath(sourceFileName), Path.GetFullPath(destFileName)) Then
158 Throw New IOException("File.Move: Failed to MoveFile.")
159 End If
160 End Sub
161
162 Static Function Open( path As String, mode As FileMode ) As FileStream
163 Return New FileStream(path, mode)
164 End Function
165
166 Static Function Open( path As String, mode As FileMode, access As FileAccess ) As FileStream
167 Return New FileStream(path, mode, access)
168 End Function
169
170 Static Function Open( path As String, mode As FileMode, access As FileAccess, share As FileShare ) As FileStream
171 Return New FileStream(path, mode, access, share)
172 End Function
173
174 Static Function OpenRead( path As String ) As FileStream
175 Return Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)
176 End Function
177
178 Static Function OpenText( path As String ) As StreamReader
179 Return New StreamReader(path)
180 End Function
181
182 Static Function OpenWrite( path As String ) As FileStream
183 Return Open(path, FileMode.Open, FileAccess.Write)
184 End Function
185
186/* Static Function ReadAllBytes( path As String ) As *Byte
187 ' TODO: 実装
188 End Function*/
189
190/* Static Function ReadAllLines( path As String ) As Strings
191 ' TODO: 実装
192 End Function */
193
194/* Static Function ReadAllLines( path As String, encoding As Encoding ) As Strings
195 ' TODO: 実装
196 End Function */
197
198/* Static Function ReadAllText( path As String ) As String
199 ' TODO: 実装
200 End Function*/
201
202/* Static Function ReadAllText( path As String, encoding As Encoding ) As String
203 ' TODO: 実装
204 End Function */
205
206/* Static Sub Replace( sourceFileName As String, destinationFileName As String, destinationBackupFileName As String )
207 ' TODO: 実装
208 End Sub*/
209
210/* Static Sub Replace( sourceFileName As String, destinationFileName As String, destinationBackupFileName As String, ignoreMetadataErrors As Boolean )
211 ' TODO: 実装
212 End Sub*/
213
214/* Static Sub SetAccessControl( path As String, fileSecurity As FileSecurity )
215 ' TODO: 実装
216 End Sub */
217
218 Static Sub SetAttributes( path As String, fileAttributes As FileAttributes )
219 If Not SetFileAttributes(ToTCStr(path), fileAttributes) Then
220 Throw New IOException("File.SetAttributes: Failed to SetFileAttributes.")
221 End If
222 End Sub
223
224 Static Sub SetCreationTime( path As String, creationTime As DateTime )
225 SetCreationTimeUtc(path, creationTime)
226 End Sub
227
228 Static Sub SetCreationTimeUtc( path As String, creationTimeUtc As DateTime )
229 Dim hFile = createFileToSetTime(path) As HANDLE
230 SetFileTime(hFile, creationTimeUtc.ToFileTimeUtc(), ByVal 0, ByVal 0)
231 CloseHandle(hFile)
232 End Sub
233
234 Static Sub SetLastAccessTime( path As String, lastAccessTime As DateTime )
235 SetLastAccessTimeUtc(path, lastAccessTime)
236 End Sub
237
238 Static Sub SetLastAccessTimeUtc( path As String, lastAccessTimeUtc As DateTime )
239 Dim hFile = createFileToSetTime(path) As HANDLE
240 SetFileTime(hFile, ByVal 0, lastAccessTimeUtc.ToFileTimeUtc(), ByVal 0)
241 CloseHandle(hFile)
242 End Sub
243
244 Static Sub SetLastWriteTime( path As String, lastWriteTime As DateTime )
245 SetLastWriteTimeUtc(path, lastWriteTime)
246 End Sub
247
248 Static Sub SetLastWriteTimeUtc( path As String, lastWriteTimeUtc As DateTime )
249 Dim hFile = createFileToSetTime(path) As HANDLE
250 SetFileTime(hFile, ByVal 0, ByVal 0, lastWriteTimeUtc.ToFileTimeUtc())
251 CloseHandle(hFile)
252 End Sub
253
254/* Static Sub WriteAllBytes( path As String, bytes As *Byte )
255 ' TODO: 実装
256 End Sub*/
257
258/* Static Sub WriteAllLines( path As String, contents As Strings )
259 ' TODO: 実装
260 End Sub */
261
262/* Static Sub WriteAllLines( path As String, contents As Strings, encoding As Enconding )
263 ' TODO: 実装
264 End Sub */
265
266/* Static Sub WriteAllText( path As String, contents As String )
267 ' TODO: 実装
268 End Sub*/
269
270/* Static Sub WriteAllText( path As String, contents As String, encoding As Enconding )
271 ' TODO: 実装
272 End Sub */
273
274Private
275
276 Static Function getFileData(path As String) As WIN32_FIND_DATA
277 Dim data As WIN32_FIND_DATA
278 Dim hFind = FindFirstFile(ToTCStr(Path.GetFullPath(path)), data)
279 FindClose(hFind)
280
281 If hFind <> INVALID_HANDLE_VALUE Then
282 Return data
283 Else
284 Throw New IOException("File.getFileData: Failed to FindFirstFile.")
285 End If
286 End Function
287
288 Static Function createFileToSetTime(path As String) As HANDLE
289 Dim hFile As HANDLE
290 hFile = CreateFile(ToTCStr(path), GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
291 If hFile = INVALID_HANDLE_VALUE Then
292 CloseHandle(hFile)
293 Throw New IOException("File.setFileTime: Failed to CreateFile.")
294 Exit Function
295 End If
296 Return hFile
297 End Function
298End Class
299
300
301End Namespace
302End Namespace
Note: See TracBrowser for help on using the repository browser.