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

Last change on this file since 325 was 325, checked in by OverTaker, 17 years ago

FileSystemInfosバグ修整。GetDirectories実装。ガベージコレクションのバグで動かない。(調査中...)

File size: 4.6 KB
Line 
1Imports System.Collections.Generic
2
3Namespace System
4Namespace IO
5
6Class DirectoryInfo
7 Inherits FileSystemInfo
8Public
9 Sub DirectoryInfo(path As String)
10 OriginalPath = path
11 FullPath = Path.GetFullPath(path)
12 Refresh()
13 End Sub
14
15 Sub ~DirectoryInfo()
16 End Sub
17
18 'Public Property
19 Function Parent() As DirectoryInfo
20 Return New DirectoryInfo(Path.GetDirectoryName(FullPath))
21 End Function
22
23 Function Root() As DirectoryInfo
24 Return New DirectoryInfo(Path.GetPathRoot(FullPath))
25 End Function
26
27 'Public Method
28 Sub Create()
29 CreateDirectory(ToTCStr(FullPath), NULL)
30 End Sub
31
32/* Sub Create(directorySecurity As DirectorySecurity)
33 End Sub*/
34
35 Override Sub Delete()
36 RemoveDirectory(ToTCStr(FullPath))
37 End Sub
38
39 Sub Delete(recursive As Boolean)
40 If recursive Then
41 ' ディレクトリ内のすべての情報を削除する
42
43 Dim dirPath = FullPath As String
44
45 ' 終端の '\' を除去
46 If dirPath[dirPath.Length-1] = Asc("\") Then
47 dirPath = dirPath.Substring(0, dirPath.Length-1)
48 End If
49
50 ' double null-terminated にする
51 dirPath = dirPath + Chr$(0)
52
53 Dim op As SHFILEOPSTRUCT
54 op.hwnd = NULL
55 op.wFunc = FO_DELETE
56 op.pFrom = ToTCStr(dirPath)
57 op.pTo = NULL
58 op.fFlags = FOF_NOCONFIRMATION or FOF_NOERRORUI or FOF_SILENT
59
60 If SHFileOperation(op) <> 0 Then
61 ' TODO: エラー処理
62 debug
63 End If
64 Else
65 ' ディレクトリが空の場合は削除する
66 This.Delete()
67 End If
68 End Sub
69
70/* Function GetAccessControl() As DirectorySecurity
71 End Function*/
72
73/* Function GetAccessControl(includeSections As AccessControlSections) As DirectorySecurity
74 End Function*/
75
76 Function GetDirectories() As List<DirectoryInfo>
77 Return GetDirectories("?*")
78 End Function
79
80 Function GetDirectories(searchPattern As String) As List<DirectoryInfo>
81 Dim infos As List<FileSystemInfo>
82 infos = GetFileSystemInfos(searchPattern)
83
84 Dim dirs As List<DirectoryInfo>
85 Dim i As Long
86 For i = 0 To ELM(infos.Count)
87 If infos[i].GetType.ToString() = "DirectoryInfo" Then
88 dirs.Add(infos[i] As DirectoryInfo)
89 End If
90 Next
91 Return dirs
92 End Function
93
94 Function GetDirectories(searchPattern As String, searchOption As SearchOption) As List<DirectoryInfo>
95 If searchOption = SearchOption.TopDirectoryOnly Then
96 Return GetDirectories(searchPattern)
97 ElseIf searchOption = SearchOption.AllDirectories Then
98 Dim dirs As List<DirectoryInfo>
99 dirs = GetDirectories(searchPattern)
100
101 Dim subdirs As List<DirectoryInfo>
102 Dim i As Long, j As Long
103 For i = 0 To ELM(dirs.Count)
104 subdirs = dirs[i].GetDirectories(searchPattern)
105 For j = 0 To ELM(subdirs.Count)
106 dirs.Add(subdirs[i])
107 Next
108 Next
109 Return dirs
110 End If
111 End Function
112
113 Function GetFiles() As List<FileInfo>
114 Return GetFiles("?*")
115 End Function
116
117 Function GetFiles(searchPattern As String) As List<FileInfo>
118 Dim infos As List<FileSystemInfo>
119 infos = GetFileSystemInfos(searchPattern)
120
121 Dim files As List<FileInfo>
122 Dim i As Long
123 For i = 0 To ELM(infos.Count)
124 If infos[i].GetType.ToString() = "FileInfo" Then
125 files.Add(infos[i] As FileInfo)
126 End If
127 Next
128 Return files
129 End Function
130
131/* Function GetFiles(searchPattern As String, searchOption As SearchOption) As List<FileInfo>
132 End Function*/
133
134 Function GetFileSystemInfos() As List<FileSystemInfo>
135 Return GetFileSystemInfos("?*")
136 End Function
137
138 Function GetFileSystemInfos(searchPattern As String) As List<FileSystemInfo>
139 Dim find As HANDLE
140 Dim findData As WIN32_FIND_DATA
141 find = FindFirstFile("?*", findData)
142 If find = INVALID_HANDLE_VALUE Then
143 debug 'Exception
144 Return Nothing
145 End If
146
147 Dim files As List<FileSystemInfo>
148 Dim lastError As DWord
149 Dim i As Long
150 While lastError <> ERROR_NO_MORE_FILES
151 If i > 1 Then
152 Dim s = New String(findData.cFileName As PCTSTR)
153 If (findData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
154 files.Add(New DirectoryInfo(Path.Combine(FullPath, s)))
155 Else
156 files.Add(New FileInfo(Path.Combine(FullPath, s)))
157 End If
158 End If
159
160 If FindNextFile(find, findData) = False Then
161 lastError = GetLastError()
162 If lastError <> ERROR_NO_MORE_FILES Then
163 FindClose(find)
164 debug 'Exception
165 Return Nothing
166 End If
167 End If
168 i++
169 Wend
170 FindClose(find)
171 Return files
172 End Function
173
174 Sub MoveTo(destDirName As String)
175 If MoveFile(ToTCStr(FullPath), ToTCStr(destDirName)) = FALSE Then
176 'Exception
177 End If
178 End Sub
179
180/* Sub SetAccessControl(directorySecurity As DirectorySecurity)
181 End Sub*/
182
183End Class
184
185End Namespace
186End Namespace
Note: See TracBrowser for help on using the repository browser.