[318] | 1 | Imports System.Collections.Generic
|
---|
[271] | 2 |
|
---|
| 3 | Namespace System
|
---|
| 4 | Namespace IO
|
---|
| 5 |
|
---|
[129] | 6 | Class DirectoryInfo
|
---|
| 7 | Inherits FileSystemInfo
|
---|
| 8 | Public
|
---|
| 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
|
---|
[318] | 20 | Return New DirectoryInfo(Path.GetDirectoryName(FullPath))
|
---|
[129] | 21 | End Function
|
---|
| 22 |
|
---|
| 23 | Function Root() As DirectoryInfo
|
---|
[318] | 24 | Return New DirectoryInfo(Path.GetPathRoot(FullPath))
|
---|
[129] | 25 | End Function
|
---|
| 26 |
|
---|
| 27 | 'Public Method
|
---|
| 28 | Sub Create()
|
---|
[142] | 29 | CreateDirectory(ToTCStr(FullPath), NULL)
|
---|
[129] | 30 | End Sub
|
---|
| 31 |
|
---|
| 32 | /* Sub Create(directorySecurity As DirectorySecurity)
|
---|
| 33 | End Sub*/
|
---|
| 34 |
|
---|
| 35 | Override Sub Delete()
|
---|
[142] | 36 | RemoveDirectory(ToTCStr(FullPath))
|
---|
[129] | 37 | End Sub
|
---|
| 38 |
|
---|
[271] | 39 | Sub Delete(recursive As Boolean)
|
---|
| 40 | If recursive Then
|
---|
| 41 | ' ディレクトリ内のすべての情報を削除する
|
---|
[129] | 42 |
|
---|
[271] | 43 | Dim dirPath = FullPath As String
|
---|
| 44 |
|
---|
| 45 | ' 終端の '\' を除去
|
---|
[285] | 46 | If dirPath[dirPath.Length-1] = Asc("\") Then
|
---|
| 47 | dirPath = dirPath.Substring(0, dirPath.Length-1)
|
---|
[271] | 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
|
---|
[285] | 56 | op.pFrom = ToTCStr(dirPath)
|
---|
[271] | 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 |
|
---|
[129] | 70 | /* Function GetAccessControl() As DirectorySecurity
|
---|
| 71 | End Function*/
|
---|
| 72 |
|
---|
| 73 | /* Function GetAccessControl(includeSections As AccessControlSections) As DirectorySecurity
|
---|
| 74 | End Function*/
|
---|
| 75 |
|
---|
[318] | 76 | Function GetDirectories() As List<DirectoryInfo>
|
---|
| 77 | Return GetDirectories("?*")
|
---|
| 78 | End Function
|
---|
[129] | 79 |
|
---|
[318] | 80 | Function GetDirectories(searchPattern As String) As List<DirectoryInfo>
|
---|
| 81 | Dim infos As List<FileSystemInfo>
|
---|
| 82 | infos = GetFileSystemInfos(searchPattern)
|
---|
[129] | 83 |
|
---|
[318] | 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
|
---|
[129] | 93 |
|
---|
[325] | 94 | Function GetDirectories(searchPattern As String, searchOption As SearchOption) As List<DirectoryInfo>
|
---|
[327] | 95 | Select Case searchOption
|
---|
| 96 | Case SearchOption.TopDirectoryOnly
|
---|
| 97 | Return GetDirectories(searchPattern)
|
---|
| 98 | Case SearchOption.AllDirectories
|
---|
| 99 | Dim dirs As List<DirectoryInfo>
|
---|
| 100 | dirs = GetDirectories(searchPattern)
|
---|
[142] | 101 |
|
---|
[327] | 102 | Dim subdirs As List<DirectoryInfo>
|
---|
| 103 | Dim i As Long, j As Long
|
---|
| 104 | For i = 0 To ELM(dirs.Count)
|
---|
| 105 | subdirs = dirs[i].GetDirectories(searchPattern)
|
---|
| 106 | For j = 0 To ELM(subdirs.Count)
|
---|
| 107 | dirs.Add(subdirs[i])
|
---|
| 108 | Next
|
---|
[321] | 109 | Next
|
---|
[327] | 110 | Return dirs
|
---|
| 111 | End Select
|
---|
[325] | 112 | End Function
|
---|
[142] | 113 |
|
---|
[318] | 114 | Function GetFiles() As List<FileInfo>
|
---|
| 115 | Return GetFiles("?*")
|
---|
| 116 | End Function
|
---|
[129] | 117 |
|
---|
[318] | 118 | Function GetFiles(searchPattern As String) As List<FileInfo>
|
---|
| 119 | Dim infos As List<FileSystemInfo>
|
---|
| 120 | infos = GetFileSystemInfos(searchPattern)
|
---|
[129] | 121 |
|
---|
[318] | 122 | Dim files As List<FileInfo>
|
---|
| 123 | Dim i As Long
|
---|
| 124 | For i = 0 To ELM(infos.Count)
|
---|
| 125 | If infos[i].GetType.ToString() = "FileInfo" Then
|
---|
| 126 | files.Add(infos[i] As FileInfo)
|
---|
| 127 | End If
|
---|
| 128 | Next
|
---|
| 129 | Return files
|
---|
| 130 | End Function
|
---|
| 131 |
|
---|
[327] | 132 | Function GetFiles(searchPattern As String, searchOption As SearchOption) As List<FileInfo>
|
---|
| 133 | Select Case searchOption
|
---|
| 134 | Case SearchOption.TopDirectoryOnly
|
---|
| 135 | Return GetFiles(searchPattern)
|
---|
| 136 | Case SearchOption.AllDirectories
|
---|
| 137 | Dim dirs As List<DirectoryInfo>
|
---|
| 138 | dirs = GetDirectories(searchPattern, SearchOption.AllDirectories)
|
---|
[129] | 139 |
|
---|
[327] | 140 | Dim files As List<FileInfo>
|
---|
| 141 | Dim subfiles As List<FileInfo>
|
---|
| 142 | Dim i As Long, j As Long
|
---|
| 143 | For i = 0 To ELM(dirs.Count)
|
---|
| 144 | subfiles = dirs[i].GetFiles(searchPattern)
|
---|
| 145 | For j = 0 To ELM(subfiles.Count)
|
---|
| 146 | files.Add(subfiles[j])
|
---|
| 147 | Next
|
---|
| 148 | Next
|
---|
| 149 | Return files
|
---|
| 150 | End Select
|
---|
| 151 | End Function
|
---|
| 152 |
|
---|
[318] | 153 | Function GetFileSystemInfos() As List<FileSystemInfo>
|
---|
| 154 | Return GetFileSystemInfos("?*")
|
---|
| 155 | End Function
|
---|
| 156 |
|
---|
| 157 | Function GetFileSystemInfos(searchPattern As String) As List<FileSystemInfo>
|
---|
| 158 | Dim find As HANDLE
|
---|
| 159 | Dim findData As WIN32_FIND_DATA
|
---|
[327] | 160 | find = FindFirstFile(ToTCStr(Path.Combine(FullPath, searchPattern)), findData)
|
---|
[318] | 161 | If find = INVALID_HANDLE_VALUE Then
|
---|
| 162 | debug 'Exception
|
---|
| 163 | Return Nothing
|
---|
| 164 | End If
|
---|
| 165 |
|
---|
| 166 | Dim files As List<FileSystemInfo>
|
---|
| 167 | Dim i As Long
|
---|
[327] | 168 | Do
|
---|
[318] | 169 | If i > 1 Then
|
---|
| 170 | Dim s = New String(findData.cFileName As PCTSTR)
|
---|
| 171 | If (findData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
|
---|
| 172 | files.Add(New DirectoryInfo(Path.Combine(FullPath, s)))
|
---|
| 173 | Else
|
---|
| 174 | files.Add(New FileInfo(Path.Combine(FullPath, s)))
|
---|
| 175 | End If
|
---|
| 176 | End If
|
---|
| 177 | i++
|
---|
[327] | 178 | Loop While FindNextFile(find, findData)
|
---|
[321] | 179 | FindClose(find)
|
---|
[327] | 180 |
|
---|
| 181 | If GetLastError() = ERROR_NO_MORE_FILES Then
|
---|
| 182 | Return files
|
---|
| 183 | Else
|
---|
| 184 | debug 'Exception
|
---|
| 185 | Return Nothing
|
---|
| 186 | End If
|
---|
[318] | 187 | End Function
|
---|
| 188 |
|
---|
[129] | 189 | Sub MoveTo(destDirName As String)
|
---|
[142] | 190 | If MoveFile(ToTCStr(FullPath), ToTCStr(destDirName)) = FALSE Then
|
---|
[129] | 191 | 'Exception
|
---|
| 192 | End If
|
---|
| 193 | End Sub
|
---|
| 194 |
|
---|
| 195 | /* Sub SetAccessControl(directorySecurity As DirectorySecurity)
|
---|
| 196 | End Sub*/
|
---|
| 197 |
|
---|
| 198 | End Class
|
---|
| 199 |
|
---|
[271] | 200 | End Namespace
|
---|
| 201 | End Namespace
|
---|