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

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

DriveInfoのコンストラクタの間違い修整。他、コメント、例外追加。

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