1 #Region "Microsoft.VisualBasic::bd8e9fe7167d5ba0c8fb9128f807d2f8, Microsoft.VisualBasic.Core\Extensions\IO\Path\Directory.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class Directory
35     
36     '         Properties: DIR
37     
38     '         Constructor: (+1 OverloadsSub New
39     
40     '         Function: CopyTo, Exists, GetFullPath, GetRelativePath, IsAbsolutePath
41     '                   ToString
42     
43     '         Sub: CreateDirectory, Delete
44     
45     
46     ' /********************************************************************************/
47
48 #End Region
49
50 Imports System.Runtime.CompilerServices
51
52 Namespace FileIO
53
54     ''' <summary>
55     ''' A wrapper object for the processing of relative file path. 
56     ''' </summary>
57     Public Class Directory
58
59         ''' <summary>
60         ''' 当前的这个文件夹对象的文件路径
61         ''' </summary>
62         ''' <returns></returns>
63         Public ReadOnly Property DIR As String
64
65         ''' <summary>
66         ''' Construct a directory object from the specific Dir path value.
67         ''' </summary>
68         ''' <param name="DIR">Target directory path</param>
69         Sub New(DIR As String)
70             Me.DIR = FileSystem.GetDirectoryInfo(DIR).FullName
71         End Sub
72
73         ''' <summary>
74         ''' Gets the full path of the target file based on the path relative to this directory object.
75         ''' </summary>
76         ''' <param name="file">
77         ''' The relative path of the target file, and this parameter is also compatible with absolute file path.
78         ''' (相对路径)</param>
79         ''' <returns></returns>
80         Public Function GetFullPath(file As StringAs String
81             If Not IsAbsolutePath(file) Then
82                 file = $"{DIR}/{file}"
83             End If
84
85             file = FileSystem.GetFileInfo(file).FullName
86             Return file
87         End Function
88
89         ''' <summary>
90         ''' Determined that the input file path is a absolute path or not?
91         ''' </summary>
92         ''' <param name="file"></param>
93         ''' <returns></returns>
94         Public Shared Function IsAbsolutePath(file As StringAs Boolean
95             If InStr(file, ":\") > 0 OrElse InStr(file, ":/") > 0 Then
96                 Return True
97             ElseIf file.First = "/" AndAlso
98                 (Environment.OSVersion.Platform = PlatformID.Unix OrElse
99                  Environment.OSVersion.Platform = PlatformID.MacOSX) Then
100                 Return True
101             Else
102                 Return False
103             End If
104         End Function
105
106         ''' <summary>
107         ''' 将当前的这个文件夹之中的内容拷贝到<paramref name="target"/>目标文件夹
108         ''' </summary>
109         ''' <param name="target">The directory path of target folder.</param>
110         ''' <returns></returns>
111         Public Function CopyTo(target$, Optional progress As Progress(Of String) = NothingOptional includeSrc As Boolean = FalseAs IEnumerable(Of String)
112             Dim list As New List(Of String)
113             Dim action = Sub(path$)
114                              If Not progress Is Nothing Then
115                                  Call DirectCast(progress, IProgress(Of String)).Report(path)
116                              End If
117
118                              Call list.Add(path)
119                          End Sub
120
121             Call New CopyDirectoryAction(New Progress(Of String)(action)).Copy(DIR, target, includeSrc)
122
123             Return list
124         End Function
125
126         Public Overrides Function ToString() As String
127             Return DIR
128         End Function
129
130         <MethodImpl(MethodImplOptions.AggressiveInlining)>
131         Public Shared Function Exists(DIR As StringAs Boolean
132             Return IO.Directory.Exists(DIR)
133         End Function
134
135         <MethodImpl(MethodImplOptions.AggressiveInlining)>
136         Public Function GetRelativePath(file As StringAs String
137             Return PathExtensions.RelativePath(DIR, file, appendParent:=False)
138         End Function
139
140         ''' <summary>
141         ''' Creates a directory.
142         ''' </summary>
143         ''' <param name="junctionPoint">Name and location of the directory.</param>
144         ''' <remarks>
145         ''' Exceptions:
146         '''   T:System.ArgumentException:
147         '''     The directory name is malformed. For example, it contains illegal characters
148         '''     or is only white space.
149         '''
150         '''   T:System.ArgumentNullException:
151         '''     directory is Nothing or an empty string.
152         '''
153         '''   T:System.IO.PathTooLongException:
154         '''     The directory name is too long.
155         '''
156         '''   T:System.NotSupportedException:
157         '''     The directory name is only a colon (:).
158         '''
159         '''   T:System.IO.IOException:
160         '''     The parent directory of the directory to be created is read-only
161         '''
162         '''   T:System.UnauthorizedAccessException:
163         '''     The user does not have permission to create the directory.
164         ''' </remarks>
165         ''' 
166         <MethodImpl(MethodImplOptions.AggressiveInlining)>
167         Public Shared Sub CreateDirectory(junctionPoint As String)
168             Call FileSystem.CreateDirectory(junctionPoint)
169         End Sub
170
171         <MethodImpl(MethodImplOptions.AggressiveInlining)>
172         Public Shared Sub Delete(DIR As String)
173             Call IO.Directory.Delete(DIR)
174         End Sub
175     End Class
176 End Namespace