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

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

System/IO/Directory.ab実装。例外処理を徐々に実装。

File size: 8.2 KB
Line 
1Imports System.Collections.Generic
2
3Namespace System
4Namespace IO
5
6Class DirectoryInfo
7    Inherits FileSystemInfo
8Public
9    /*!
10    @brief  コンストラクタ
11    @author OverTaker
12    @date   2007/11/11
13    @param  ディレクトリのパス
14    */
15    Sub DirectoryInfo(path As String)
16        OriginalPath = path
17        FullPath = Path.GetFullPath(path)
18    End Sub
19
20    Sub ~DirectoryInfo()
21    End Sub
22
23    '----------------------------------------------------------------
24    ' パブリック プロパティ
25    '----------------------------------------------------------------
26
27    /*!
28    @brief  ひとつ上のディレクトリを取得する
29    @author OverTaker
30    @date   2007/11/11
31    @return 親ディレクトリ
32    */
33    Function Parent() As DirectoryInfo
34        Return New DirectoryInfo(Path.GetDirectoryName(FullPath))
35    End Function
36
37    /*!
38    @brief  ルートディレクトリを取得する
39    @author OverTaker
40    @date   2007/11/11
41    @return ルートディレクトリ
42    */
43    Function Root() As DirectoryInfo
44        Return New DirectoryInfo(Path.GetPathRoot(FullPath))
45    End Function
46
47    '----------------------------------------------------------------
48    ' パブリック メソッド
49    '----------------------------------------------------------------
50
51    /*!
52    @brief  ディレクトリを作成する
53    @author OverTaker
54    @date   2007/11/11
55    */
56    Sub Create()
57        If CreateDirectory(ToTCStr(FullPath), NULL) = False Then
58            Dim error = GetLastError()
59            Select Case error
60                Case ERROR_ALREADY_EXISTS
61                    Throw New IOException("DirectoryInfo.CreateDirectory: The directory has already existed.")
62                Case Else
63                    Throw New IOException("DirectoryInfo.CreateDirectory: Failed to CreateDirectory")
64
65            End Select
66        End If
67    End Sub
68
69/*  Sub Create(directorySecurity As DirectorySecurity)
70    End Sub*/
71
72    /*!
73    @brief  ディレクトリを削除する。ただしディレクトリが空の場合
74    @author OverTaker
75    @date   2007/11/11
76    */
77    Override Sub Delete()
78        RemoveDirectory(ToTCStr(FullPath))
79    End Sub
80
81    /*!
82    @brief  ディレクトリを削除する
83    @author OverTaker
84    @date   2007/11/11
85    @param  ディレクトリのファイルごと消すかどうか
86    */
87    Sub Delete(recursive As Boolean)
88        If recursive Then
89            ' ディレクトリ内のすべての情報を削除する
90
91            Dim dirPath = FullPath As String
92
93            ' 終端の '\' を除去
94            If dirPath[dirPath.Length-1] = Asc("\") Then
95                dirPath = dirPath.Substring(0, dirPath.Length-1)
96            End If
97
98            ' double null-terminated にする
99            dirPath = dirPath + Chr$(0)
100
101            Dim op As SHFILEOPSTRUCT
102            op.hwnd = NULL
103            op.wFunc = FO_DELETE
104            op.pFrom = ToTCStr(dirPath)
105            op.pTo = NULL
106            op.fFlags = FOF_NOCONFIRMATION or FOF_NOERRORUI or FOF_SILENT
107
108            If SHFileOperation(op) <> 0 Then
109                Throw New IOException("DirectoryInfo.Delete: Failed to SHFileOperation.")
110            End If
111        Else
112            ' ディレクトリが空の場合は削除する
113            This.Delete()
114        End If
115    End Sub
116
117/*  Function GetAccessControl() As DirectorySecurity
118    End Function*/
119
120/*  Function GetAccessControl(includeSections As AccessControlSections) As DirectorySecurity
121    End Function*/
122
123    /*!
124    @brief  ディレクトリの中にあるディレクトリを取得する
125    @author OverTaker
126    @date   2007/11/11
127    @return ディレクトリの配列
128    */
129    Function GetDirectories() As List<DirectoryInfo>
130        Return GetDirectories("?*")
131    End Function
132
133    /*!
134    @brief  ディレクトリの中にあるディレクトリを取得する
135    @author OverTaker
136    @date   2007/11/11
137    @param  サーチするディレクトリ名のパターン
138    @return パターンに適合したディレクトリの配列
139    */
140    Function GetDirectories(searchPattern As String) As List<DirectoryInfo>
141        Dim infos As List<FileSystemInfo>
142        infos = GetFileSystemInfos(searchPattern)
143
144        Dim dirs As List<DirectoryInfo>
145        Dim i As Long
146        For i = 0 To ELM(infos.Count)
147            If (infos[i].Attributes and FileAttributes.Directory) = FileAttributes.Directory Then
148                dirs.Add(infos[i] As DirectoryInfo)
149            End If
150        Next
151        Return dirs
152    End Function
153
154    /*!
155    @brief  ディレクトリの中にあるディレクトリを取得する
156    @author OverTaker
157    @date   2007/11/11
158    @param  サーチするディレクトリ名のパターン
159    @param  サーチする範囲
160    @return サーチした範囲にあるパターンに適合したディレクトリの配列
161    */
162    Function GetDirectories(searchPattern As String, searchOption As SearchOption) As List<DirectoryInfo>
163        Select Case searchOption
164            Case SearchOption.TopDirectoryOnly
165                Return GetDirectories(searchPattern)
166            Case SearchOption.AllDirectories
167                Dim dirs As List<DirectoryInfo>
168                dirs = GetDirectories(searchPattern)
169
170                Dim subdirs As List<DirectoryInfo>
171                Dim i As Long, j As Long
172                For i = 0 To ELM(dirs.Count)
173                    subdirs = dirs[i].GetDirectories(searchPattern)
174                    For j = 0 To ELM(subdirs.Count)
175                        dirs.Add(subdirs[j])
176                    Next
177                Next
178                Return dirs
179        End Select
180    End Function
181
182    /*!
183    @brief  ディレクトリの中にあるファイルを取得する
184    @author OverTaker
185    @date   2007/11/11
186    @return ファイルの配列
187    */
188    Function GetFiles() As List<FileInfo>
189        Return GetFiles("?*")
190    End Function
191
192    /*!
193    @brief  ディレクトリの中にあるファイルを取得する
194    @author OverTaker
195    @date   2007/11/11
196    @param  サーチするファイル名のパターン
197    @return パターンに適合したファイルの配列
198    */
199    Function GetFiles(searchPattern As String) As List<FileInfo>
200        Dim infos As List<FileSystemInfo>
201        infos = GetFileSystemInfos(searchPattern)
202
203        Dim files As List<FileInfo>
204        Dim i As Long
205        For i = 0 To ELM(infos.Count)
206            If (infos[i].Attributes and FileAttributes.Directory) <> FileAttributes.Directory Then
207                files.Add(infos[i] As FileInfo)
208            End If
209        Next
210        Return files
211    End Function
212
213    /*!
214    @brief  ディレクトリの中にあるファイルを取得する
215    @author OverTaker
216    @date   2007/11/11
217    @param  サーチするファイル名のパターン
218    @param  サーチする範囲
219    @return サーチした範囲にあるパターンに適合したディレクトリの配列
220    */
221    Function GetFiles(searchPattern As String, searchOption As SearchOption) As List<FileInfo>
222        Select Case searchOption
223            Case SearchOption.TopDirectoryOnly
224                Return GetFiles(searchPattern)
225            Case SearchOption.AllDirectories
226                Dim dirs As List<DirectoryInfo>
227                dirs = GetDirectories("?*", SearchOption.AllDirectories)
228
229                Dim files As List<FileInfo>
230                files = GetFiles(searchPattern)
231                Dim i As Long, j As Long, subfiles As List<FileInfo>
232                For i = 0 To ELM(dirs.Count)
233                    subfiles = dirs[i].GetFiles(searchPattern)
234                    For j = 0 To ELM(subfiles.Count)
235                        files.Add(subfiles[j])
236                    Next
237                Next
238                Return files
239        End Select
240    End Function
241
242    /*!
243    @brief  ディレクトリの中にあるディレクトリやファイルを取得する
244    @author OverTaker
245    @date   2007/11/11
246    @return ディレクトリやファイルの配列
247    */
248    Function GetFileSystemInfos() As List<FileSystemInfo>
249        Return GetFileSystemInfos("?*")
250    End Function
251
252    /*!
253    @brief  ディレクトリの中にあるディレクトリやファイルを取得する
254    @author OverTaker
255    @date   2007/11/11
256    @param  サーチする名前のパターン
257    @return パターンに適合したディレクトリやファイルの配列
258    */
259    Function GetFileSystemInfos(searchPattern As String) As List<FileSystemInfo>
260        Dim find As HANDLE
261        Dim findData As WIN32_FIND_DATA
262        find = FindFirstFile(ToTCStr(Path.Combine(FullPath, searchPattern)), findData)
263        If find = INVALID_HANDLE_VALUE Then
264            Throw New IOException("DirectoryInfo.GetFileSystemInfos: Failed to FindFirstFile.")
265            Return Nothing
266        End If
267
268        Dim files As List<FileSystemInfo>
269        Do
270            Dim s = New String(findData.cFileName As PCTSTR)
271            If (findData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
272                files.Add(New DirectoryInfo(Path.Combine(FullPath, s)))
273            Else
274                files.Add(New FileInfo(Path.Combine(FullPath, s)))
275            End If
276        Loop While FindNextFile(find, findData)
277        FindClose(find)
278
279        files.Remove(New DirectoryInfo(Path.Combine(FullPath, ".")))
280        files.Remove(New DirectoryInfo(Path.Combine(FullPath, "..")))
281
282        If GetLastError() = ERROR_NO_MORE_FILES Then
283            Return files
284        Else
285            Throw New IOException("DirectoryInfo.GetFileSystemInfos: Failed to FindNextFile.")
286            Return Nothing
287        End If
288    End Function
289
290    /*!
291    @brief  ディレクトリを移動する
292    @author OverTaker
293    @date   2007/11/11
294    @param  移動先
295    */
296    Sub MoveTo(destDirName As String)
297        If MoveFile(ToTCStr(FullPath), ToTCStr(destDirName)) = FALSE Then
298            Throw New IOException("DirectoryInfo.MoveTo: Failed to MoveFile.")
299        End If
300    End Sub
301
302/*  Sub SetAccessControl(directorySecurity As DirectorySecurity)
303    End Sub*/
304
305End Class
306
307End Namespace
308End Namespace
Note: See TracBrowser for help on using the repository browser.