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