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