[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>
|
---|
[318] | 95 | If searchOption = SearchOption.TopDirectoryOnly Then
|
---|
| 96 | Return GetDirectories(searchPattern)
|
---|
[321] | 97 | ElseIf searchOption = SearchOption.AllDirectories Then
|
---|
[318] | 98 | Dim dirs As List<DirectoryInfo>
|
---|
| 99 | dirs = GetDirectories(searchPattern)
|
---|
[142] | 100 |
|
---|
[321] | 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
|
---|
[318] | 110 | End If
|
---|
[325] | 111 | End Function
|
---|
[142] | 112 |
|
---|
[318] | 113 | Function GetFiles() As List<FileInfo>
|
---|
| 114 | Return GetFiles("?*")
|
---|
| 115 | End Function
|
---|
[129] | 116 |
|
---|
[318] | 117 | Function GetFiles(searchPattern As String) As List<FileInfo>
|
---|
| 118 | Dim infos As List<FileSystemInfo>
|
---|
| 119 | infos = GetFileSystemInfos(searchPattern)
|
---|
[129] | 120 |
|
---|
[318] | 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>
|
---|
[129] | 132 | End Function*/
|
---|
| 133 |
|
---|
[318] | 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
|
---|
[325] | 141 | find = FindFirstFile("?*", findData)
|
---|
[318] | 142 | If find = INVALID_HANDLE_VALUE Then
|
---|
| 143 | debug 'Exception
|
---|
| 144 | Return Nothing
|
---|
| 145 | End If
|
---|
| 146 |
|
---|
| 147 | Dim files As List<FileSystemInfo>
|
---|
[325] | 148 | Dim lastError As DWord
|
---|
[318] | 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
|
---|
[321] | 163 | FindClose(find)
|
---|
[318] | 164 | debug 'Exception
|
---|
| 165 | Return Nothing
|
---|
| 166 | End If
|
---|
| 167 | End If
|
---|
| 168 | i++
|
---|
| 169 | Wend
|
---|
[321] | 170 | FindClose(find)
|
---|
[318] | 171 | Return files
|
---|
| 172 | End Function
|
---|
| 173 |
|
---|
[129] | 174 | Sub MoveTo(destDirName As String)
|
---|
[142] | 175 | If MoveFile(ToTCStr(FullPath), ToTCStr(destDirName)) = FALSE Then
|
---|
[129] | 176 | 'Exception
|
---|
| 177 | End If
|
---|
| 178 | End Sub
|
---|
| 179 |
|
---|
| 180 | /* Sub SetAccessControl(directorySecurity As DirectorySecurity)
|
---|
| 181 | End Sub*/
|
---|
| 182 |
|
---|
| 183 | End Class
|
---|
| 184 |
|
---|
[271] | 185 | End Namespace
|
---|
| 186 | End Namespace
|
---|