[318] | 1 | Imports System.Collections.Generic
|
---|
[271] | 2 |
|
---|
| 3 | Namespace System
|
---|
| 4 | Namespace IO
|
---|
| 5 |
|
---|
[129] | 6 | Class DirectoryInfo
|
---|
| 7 | Inherits FileSystemInfo
|
---|
| 8 | Public
|
---|
[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()
|
---|
[142] | 57 | CreateDirectory(ToTCStr(FullPath), NULL)
|
---|
[129] | 58 | End Sub
|
---|
| 59 |
|
---|
| 60 | /* Sub Create(directorySecurity As DirectorySecurity)
|
---|
| 61 | End Sub*/
|
---|
| 62 |
|
---|
[404] | 63 | /*!
|
---|
| 64 | @brief ディレクトリを削除する。ただしディレクトリが空の場合
|
---|
| 65 | @author OverTaker
|
---|
| 66 | @date 2007/11/11
|
---|
| 67 | */
|
---|
[129] | 68 | Override Sub Delete()
|
---|
[142] | 69 | RemoveDirectory(ToTCStr(FullPath))
|
---|
[129] | 70 | End Sub
|
---|
| 71 |
|
---|
[404] | 72 | /*!
|
---|
| 73 | @brief ディレクトリを削除する
|
---|
| 74 | @author OverTaker
|
---|
| 75 | @date 2007/11/11
|
---|
| 76 | @param ディレクトリのファイルごと消すかどうか
|
---|
| 77 | */
|
---|
[271] | 78 | Sub Delete(recursive As Boolean)
|
---|
| 79 | If recursive Then
|
---|
| 80 | ' ディレクトリ内のすべての情報を削除する
|
---|
[129] | 81 |
|
---|
[271] | 82 | Dim dirPath = FullPath As String
|
---|
| 83 |
|
---|
| 84 | ' 終端の '\' を除去
|
---|
[285] | 85 | If dirPath[dirPath.Length-1] = Asc("\") Then
|
---|
| 86 | dirPath = dirPath.Substring(0, dirPath.Length-1)
|
---|
[271] | 87 | End If
|
---|
| 88 |
|
---|
| 89 | ' double null-terminated にする
|
---|
| 90 | dirPath = dirPath + Chr$(0)
|
---|
| 91 |
|
---|
| 92 | Dim op As SHFILEOPSTRUCT
|
---|
| 93 | op.hwnd = NULL
|
---|
| 94 | op.wFunc = FO_DELETE
|
---|
[285] | 95 | op.pFrom = ToTCStr(dirPath)
|
---|
[271] | 96 | op.pTo = NULL
|
---|
| 97 | op.fFlags = FOF_NOCONFIRMATION or FOF_NOERRORUI or FOF_SILENT
|
---|
| 98 |
|
---|
| 99 | If SHFileOperation(op) <> 0 Then
|
---|
| 100 | ' TODO: エラー処理
|
---|
| 101 | debug
|
---|
| 102 | End If
|
---|
| 103 | Else
|
---|
| 104 | ' ディレクトリが空の場合は削除する
|
---|
| 105 | This.Delete()
|
---|
| 106 | End If
|
---|
| 107 | End Sub
|
---|
| 108 |
|
---|
[129] | 109 | /* Function GetAccessControl() As DirectorySecurity
|
---|
| 110 | End Function*/
|
---|
| 111 |
|
---|
| 112 | /* Function GetAccessControl(includeSections As AccessControlSections) As DirectorySecurity
|
---|
| 113 | End Function*/
|
---|
| 114 |
|
---|
[404] | 115 | /*!
|
---|
| 116 | @brief ディレクトリの中にあるディレクトリを取得する
|
---|
| 117 | @author OverTaker
|
---|
| 118 | @date 2007/11/11
|
---|
| 119 | @return ディレクトリの配列
|
---|
| 120 | */
|
---|
[318] | 121 | Function GetDirectories() As List<DirectoryInfo>
|
---|
| 122 | Return GetDirectories("?*")
|
---|
| 123 | End Function
|
---|
[129] | 124 |
|
---|
[404] | 125 | /*!
|
---|
| 126 | @brief ディレクトリの中にあるディレクトリを取得する
|
---|
| 127 | @author OverTaker
|
---|
| 128 | @date 2007/11/11
|
---|
| 129 | @param サーチするディレクトリ名のパターン
|
---|
| 130 | @return パターンに適合したディレクトリの配列
|
---|
| 131 | */
|
---|
[318] | 132 | Function GetDirectories(searchPattern As String) As List<DirectoryInfo>
|
---|
| 133 | Dim infos As List<FileSystemInfo>
|
---|
| 134 | infos = GetFileSystemInfos(searchPattern)
|
---|
[129] | 135 |
|
---|
[318] | 136 | Dim dirs As List<DirectoryInfo>
|
---|
| 137 | Dim i As Long
|
---|
| 138 | For i = 0 To ELM(infos.Count)
|
---|
[406] | 139 | If (infos[i].Attributes and FileAttributes.Directory) = FileAttributes.Directory Then
|
---|
[318] | 140 | dirs.Add(infos[i] As DirectoryInfo)
|
---|
| 141 | End If
|
---|
| 142 | Next
|
---|
| 143 | Return dirs
|
---|
| 144 | End Function
|
---|
[129] | 145 |
|
---|
[404] | 146 | /*!
|
---|
| 147 | @brief ディレクトリの中にあるディレクトリを取得する
|
---|
| 148 | @author OverTaker
|
---|
| 149 | @date 2007/11/11
|
---|
| 150 | @param サーチするディレクトリ名のパターン
|
---|
| 151 | @param サーチする範囲
|
---|
| 152 | @return サーチした範囲にあるパターンに適合したディレクトリの配列
|
---|
| 153 | */
|
---|
[325] | 154 | Function GetDirectories(searchPattern As String, searchOption As SearchOption) As List<DirectoryInfo>
|
---|
[327] | 155 | Select Case searchOption
|
---|
| 156 | Case SearchOption.TopDirectoryOnly
|
---|
| 157 | Return GetDirectories(searchPattern)
|
---|
| 158 | Case SearchOption.AllDirectories
|
---|
| 159 | Dim dirs As List<DirectoryInfo>
|
---|
| 160 | dirs = GetDirectories(searchPattern)
|
---|
[142] | 161 |
|
---|
[327] | 162 | Dim subdirs As List<DirectoryInfo>
|
---|
| 163 | Dim i As Long, j As Long
|
---|
| 164 | For i = 0 To ELM(dirs.Count)
|
---|
| 165 | subdirs = dirs[i].GetDirectories(searchPattern)
|
---|
| 166 | For j = 0 To ELM(subdirs.Count)
|
---|
[333] | 167 | dirs.Add(subdirs[j])
|
---|
[327] | 168 | Next
|
---|
[321] | 169 | Next
|
---|
[327] | 170 | Return dirs
|
---|
| 171 | End Select
|
---|
[325] | 172 | End Function
|
---|
[142] | 173 |
|
---|
[404] | 174 | /*!
|
---|
| 175 | @brief ディレクトリの中にあるファイルを取得する
|
---|
| 176 | @author OverTaker
|
---|
| 177 | @date 2007/11/11
|
---|
| 178 | @return ファイルの配列
|
---|
| 179 | */
|
---|
[318] | 180 | Function GetFiles() As List<FileInfo>
|
---|
| 181 | Return GetFiles("?*")
|
---|
| 182 | End Function
|
---|
[129] | 183 |
|
---|
[404] | 184 | /*!
|
---|
| 185 | @brief ディレクトリの中にあるファイルを取得する
|
---|
| 186 | @author OverTaker
|
---|
| 187 | @date 2007/11/11
|
---|
| 188 | @param サーチするファイル名のパターン
|
---|
| 189 | @return パターンに適合したファイルの配列
|
---|
| 190 | */
|
---|
[318] | 191 | Function GetFiles(searchPattern As String) As List<FileInfo>
|
---|
| 192 | Dim infos As List<FileSystemInfo>
|
---|
| 193 | infos = GetFileSystemInfos(searchPattern)
|
---|
[129] | 194 |
|
---|
[318] | 195 | Dim files As List<FileInfo>
|
---|
| 196 | Dim i As Long
|
---|
| 197 | For i = 0 To ELM(infos.Count)
|
---|
[406] | 198 | If (infos[i].Attributes and FileAttributes.Directory) <> FileAttributes.Directory Then
|
---|
[318] | 199 | files.Add(infos[i] As FileInfo)
|
---|
| 200 | End If
|
---|
| 201 | Next
|
---|
| 202 | Return files
|
---|
| 203 | End Function
|
---|
| 204 |
|
---|
[404] | 205 | /*!
|
---|
| 206 | @brief ディレクトリの中にあるファイルを取得する
|
---|
| 207 | @author OverTaker
|
---|
| 208 | @date 2007/11/11
|
---|
| 209 | @param サーチするファイル名のパターン
|
---|
| 210 | @param サーチする範囲
|
---|
| 211 | @return サーチした範囲にあるパターンに適合したディレクトリの配列
|
---|
| 212 | */
|
---|
[327] | 213 | Function GetFiles(searchPattern As String, searchOption As SearchOption) As List<FileInfo>
|
---|
| 214 | Select Case searchOption
|
---|
| 215 | Case SearchOption.TopDirectoryOnly
|
---|
| 216 | Return GetFiles(searchPattern)
|
---|
| 217 | Case SearchOption.AllDirectories
|
---|
| 218 | Dim dirs As List<DirectoryInfo>
|
---|
[333] | 219 | dirs = GetDirectories("?*", SearchOption.AllDirectories)
|
---|
[129] | 220 |
|
---|
[327] | 221 | Dim files As List<FileInfo>
|
---|
[333] | 222 | files = GetFiles(searchPattern)
|
---|
| 223 | Dim i As Long, j As Long, subfiles As List<FileInfo>
|
---|
[327] | 224 | For i = 0 To ELM(dirs.Count)
|
---|
| 225 | subfiles = dirs[i].GetFiles(searchPattern)
|
---|
| 226 | For j = 0 To ELM(subfiles.Count)
|
---|
| 227 | files.Add(subfiles[j])
|
---|
| 228 | Next
|
---|
| 229 | Next
|
---|
| 230 | Return files
|
---|
| 231 | End Select
|
---|
| 232 | End Function
|
---|
| 233 |
|
---|
[404] | 234 | /*!
|
---|
| 235 | @brief ディレクトリの中にあるディレクトリやファイルを取得する
|
---|
| 236 | @author OverTaker
|
---|
| 237 | @date 2007/11/11
|
---|
| 238 | @return ディレクトリやファイルの配列
|
---|
| 239 | */
|
---|
[318] | 240 | Function GetFileSystemInfos() As List<FileSystemInfo>
|
---|
| 241 | Return GetFileSystemInfos("?*")
|
---|
| 242 | End Function
|
---|
| 243 |
|
---|
[404] | 244 | /*!
|
---|
| 245 | @brief ディレクトリの中にあるディレクトリやファイルを取得する
|
---|
| 246 | @author OverTaker
|
---|
| 247 | @date 2007/11/11
|
---|
| 248 | @param サーチする名前のパターン
|
---|
| 249 | @return パターンに適合したディレクトリやファイルの配列
|
---|
| 250 | */
|
---|
[318] | 251 | Function GetFileSystemInfos(searchPattern As String) As List<FileSystemInfo>
|
---|
| 252 | Dim find As HANDLE
|
---|
| 253 | Dim findData As WIN32_FIND_DATA
|
---|
[327] | 254 | find = FindFirstFile(ToTCStr(Path.Combine(FullPath, searchPattern)), findData)
|
---|
[318] | 255 | If find = INVALID_HANDLE_VALUE Then
|
---|
[345] | 256 | Return New List<DirectoryInfo>
|
---|
[318] | 257 | End If
|
---|
| 258 |
|
---|
| 259 | Dim files As List<FileSystemInfo>
|
---|
[327] | 260 | Do
|
---|
[345] | 261 | Dim s = New String(findData.cFileName As PCTSTR)
|
---|
| 262 | If (findData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
|
---|
| 263 | files.Add(New DirectoryInfo(Path.Combine(FullPath, s)))
|
---|
| 264 | Else
|
---|
| 265 | files.Add(New FileInfo(Path.Combine(FullPath, s)))
|
---|
[318] | 266 | End If
|
---|
[327] | 267 | Loop While FindNextFile(find, findData)
|
---|
[321] | 268 | FindClose(find)
|
---|
[327] | 269 |
|
---|
[345] | 270 | files.Remove(New DirectoryInfo(Path.Combine(FullPath, ".")))
|
---|
| 271 | files.Remove(New DirectoryInfo(Path.Combine(FullPath, "..")))
|
---|
| 272 |
|
---|
[327] | 273 | If GetLastError() = ERROR_NO_MORE_FILES Then
|
---|
| 274 | Return files
|
---|
| 275 | Else
|
---|
| 276 | debug 'Exception
|
---|
| 277 | Return Nothing
|
---|
| 278 | End If
|
---|
[318] | 279 | End Function
|
---|
| 280 |
|
---|
[404] | 281 | /*!
|
---|
| 282 | @brief ディレクトリを移動する
|
---|
| 283 | @author OverTaker
|
---|
| 284 | @date 2007/11/11
|
---|
| 285 | @param 移動先
|
---|
| 286 | */
|
---|
[129] | 287 | Sub MoveTo(destDirName As String)
|
---|
[142] | 288 | If MoveFile(ToTCStr(FullPath), ToTCStr(destDirName)) = FALSE Then
|
---|
[129] | 289 | 'Exception
|
---|
| 290 | End If
|
---|
| 291 | End Sub
|
---|
| 292 |
|
---|
| 293 | /* Sub SetAccessControl(directorySecurity As DirectorySecurity)
|
---|
| 294 | End Sub*/
|
---|
| 295 |
|
---|
| 296 | End Class
|
---|
| 297 |
|
---|
[271] | 298 | End Namespace
|
---|
| 299 | End Namespace
|
---|