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