microsoft-visualbasic-runtime/Extensions/IO/Path/Directory.vb

182 lines
6.5 KiB
VB.net
Raw Normal View History

2020-03-28 15:44:37 +08:00
#Region "Microsoft.VisualBasic::fd4eea6a026c054e1b97ca8c78f98bd8, Microsoft.VisualBasic.Core\Extensions\IO\Path\Directory.vb"
2018-08-02 20:14:48 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Class Directory
'
2020-03-28 15:44:37 +08:00
' Properties: folder
2018-08-02 20:14:48 +08:00
'
' Constructor: (+1 Overloads) Sub New
'
2019-01-18 22:48:45 +08:00
' Function: CopyTo, Exists, GetFullPath, GetRelativePath, GetSubDirectories
' IsAbsolutePath, ToString
2018-08-02 20:14:48 +08:00
'
' Sub: CreateDirectory, Delete
'
'
' /********************************************************************************/
#End Region
Imports System.Runtime.CompilerServices
Namespace FileIO
''' <summary>
''' A wrapper object for the processing of relative file path.
''' </summary>
Public Class Directory
2018-10-26 21:51:15 +08:00
''' <summary>
''' 当前的这个文件夹对象的文件路径
''' </summary>
''' <returns></returns>
2020-01-12 22:52:18 +08:00
Public ReadOnly Property folder As String
2018-08-02 20:14:48 +08:00
''' <summary>
''' Construct a directory object from the specific Dir path value.
''' </summary>
2020-01-12 22:52:18 +08:00
''' <param name="directory">Target directory path</param>
Sub New(directory As String)
Me.folder = FileSystem.GetDirectoryInfo(directory).FullName
2018-08-02 20:14:48 +08:00
End Sub
2018-12-31 20:40:33 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Function GetSubDirectories() As IEnumerable(Of String)
2020-01-12 22:52:18 +08:00
Return folder.ListDirectory
2018-12-31 20:40:33 +08:00
End Function
2018-08-02 20:14:48 +08:00
''' <summary>
''' Gets the full path of the target file based on the path relative to this directory object.
''' </summary>
''' <param name="file">
''' The relative path of the target file, and this parameter is also compatible with absolute file path.
''' (相对路径)</param>
''' <returns></returns>
Public Function GetFullPath(file As String) As String
If Not IsAbsolutePath(file) Then
2020-01-12 22:52:18 +08:00
file = $"{folder}/{file}"
2018-08-02 20:14:48 +08:00
End If
file = FileSystem.GetFileInfo(file).FullName
Return file
End Function
''' <summary>
''' Determined that the input file path is a absolute path or not?
''' </summary>
''' <param name="file"></param>
''' <returns></returns>
Public Shared Function IsAbsolutePath(file As String) As Boolean
If InStr(file, ":\") > 0 OrElse InStr(file, ":/") > 0 Then
Return True
ElseIf file.First = "/" AndAlso
(Environment.OSVersion.Platform = PlatformID.Unix OrElse
2018-10-26 21:51:15 +08:00
Environment.OSVersion.Platform = PlatformID.MacOSX) Then
2018-08-02 20:14:48 +08:00
Return True
Else
Return False
End If
End Function
''' <summary>
''' 将当前的这个文件夹之中的内容拷贝到<paramref name="target"/>目标文件夹
''' </summary>
''' <param name="target">The directory path of target folder.</param>
''' <returns></returns>
Public Function CopyTo(target$, Optional progress As Progress(Of String) = Nothing, Optional includeSrc As Boolean = False) As IEnumerable(Of String)
Dim list As New List(Of String)
Dim action = Sub(path$)
If Not progress Is Nothing Then
Call DirectCast(progress, IProgress(Of String)).Report(path)
End If
Call list.Add(path)
End Sub
2020-01-12 22:52:18 +08:00
Call New CopyDirectoryAction(New Progress(Of String)(action)).Copy(folder, target, includeSrc)
2018-08-02 20:14:48 +08:00
Return list
End Function
Public Overrides Function ToString() As String
2020-01-12 22:52:18 +08:00
Return folder
2018-08-02 20:14:48 +08:00
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Shared Function Exists(DIR As String) As Boolean
Return IO.Directory.Exists(DIR)
End Function
2018-10-26 21:51:15 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Function GetRelativePath(file As String) As String
2020-01-12 22:52:18 +08:00
Return PathExtensions.RelativePath(folder, file, appendParent:=False)
2018-10-26 21:51:15 +08:00
End Function
2018-08-02 20:14:48 +08:00
''' <summary>
''' Creates a directory.
''' </summary>
''' <param name="junctionPoint">Name and location of the directory.</param>
''' <remarks>
''' Exceptions:
''' T:System.ArgumentException:
''' The directory name is malformed. For example, it contains illegal characters
''' or is only white space.
'''
''' T:System.ArgumentNullException:
''' directory is Nothing or an empty string.
'''
''' T:System.IO.PathTooLongException:
''' The directory name is too long.
'''
''' T:System.NotSupportedException:
''' The directory name is only a colon (:).
'''
''' T:System.IO.IOException:
''' The parent directory of the directory to be created is read-only
'''
''' T:System.UnauthorizedAccessException:
''' The user does not have permission to create the directory.
''' </remarks>
'''
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Shared Sub CreateDirectory(junctionPoint As String)
Call FileSystem.CreateDirectory(junctionPoint)
End Sub
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Shared Sub Delete(DIR As String)
Call IO.Directory.Delete(DIR)
End Sub
End Class
End Namespace