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

Last change on this file since 406 was 406, checked in by OverTaker, 15 years ago

FileSystemInfo?.FileAttributesを修整

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