📄 filesystemoperation.vb
字号:
'' FileSystemOperation.vb'' Authors:' Rolf Bjarne Kvinge (RKvinge@novell.com>'' Copyright (C) 2007 Novell (http://www.novell.com)'' Permission is hereby granted, free of charge, to any person obtaining' a copy of this software and associated documentation files (the' "Software"), to deal in the Software without restriction, including' without limitation the rights to use, copy, modify, merge, publish,' distribute, sublicense, and/or sell copies of the Software, and to' permit persons to whom the Software is furnished to do so, subject to' the following conditions:' ' The above copyright notice and this permission notice shall be' included in all copies or substantial portions of the Software.' ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,' EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF' MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE' LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION' OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.'#If NET_2_0 ThenImports System.IOImports System.TextImports System.Collections.ObjectModelImports System.Collections.GenericNamespace Microsoft.VisualBasic.FileIO Friend Class FileSystemOperation Private m_Source As String Private m_Destination As String Private m_Overwrite As Boolean Private m_DirectoryNotEmpty As DeleteDirectoryOption Private m_ShowUI As Boolean Private m_ShowUIOption As UIOption Private m_UICancelOption As UICancelOption Private m_Recycle As RecycleOption#If TARGET_JVM = False Then 'Windows.Forms Not Supported by Grasshopper Private m_UI As FileSystemOperationUI#End If Private m_Sources As New Generic.List(Of Info) Private m_TotalSize As Long Private m_Cancelled As Boolean Private m_Errors As New Generic.Dictionary(Of String, String) Private Class Info Public Name As String Public Size As Long Public IsDir As Boolean End Class#Region "Constructors" Sub New(ByVal Source As String, ByVal Destination As String, ByVal ShowUIOption As UIOption, ByVal UICancelOption As UICancelOption, ByVal ShowUI As Boolean, ByVal overwrite As Boolean) Me.m_Source = Source Me.m_Destination = Destination Me.m_ShowUI = ShowUI Me.m_ShowUIOption = ShowUIOption Me.m_UICancelOption = UICancelOption Me.m_Overwrite = overwrite End Sub Sub New(ByVal Source As String, ByVal Destination As String, ByVal ShowUIOption As UIOption, ByVal UICancelOption As UICancelOption) Me.m_Source = Source Me.m_Destination = Destination Me.m_ShowUI = True Me.m_ShowUIOption = ShowUIOption Me.m_UICancelOption = UICancelOption Me.m_Overwrite = False End Sub Sub New(ByVal Source As String, ByVal Destination As String, ByVal Overwrite As Boolean) Me.m_Source = Source Me.m_Destination = Destination Me.m_ShowUI = False Me.m_Overwrite = Overwrite End Sub Sub New(ByVal Directory As String, ByVal ShowUIOption As UIOption, ByVal Recycle As RecycleOption, ByVal UICancelOption As UICancelOption, ByVal onDirectoryNotEmpty As DeleteDirectoryOption, ByVal ShowUI As Boolean) Me.m_Source = Directory Me.m_ShowUI = ShowUI Me.m_ShowUIOption = ShowUIOption Me.m_UICancelOption = UICancelOption Me.m_Recycle = Recycle Me.m_DirectoryNotEmpty = onDirectoryNotEmpty Me.m_Overwrite = False End Sub Sub New(ByVal Directory As String, ByVal ShowUIOption As UIOption, ByVal Recycle As RecycleOption, ByVal UICancelOption As UICancelOption, ByVal ShowUI As Boolean) Me.m_Source = Directory Me.m_ShowUI = ShowUI Me.m_ShowUIOption = ShowUIOption Me.m_UICancelOption = UICancelOption Me.m_Recycle = Recycle Me.m_Overwrite = False End Sub#End Region#Region "Public Methods" Sub Cancel() m_Cancelled = True End Sub Sub ExecuteFileCopy() Try Init("Copy") LoadSources(False, True) Copy() Finally CleanUp() End Try End Sub Sub ExecuteFileMove() Try Init("Move") LoadSources(False, True) Move() Finally CleanUp() End Try End Sub Sub ExecuteDirCopy() Try Init("Copy") LoadSources(True, True) Copy() Finally CleanUp() End Try End Sub Sub ExecuteDirMove() Try Init("Move") If IO.Directory.Exists(m_Source) = False Then Throw New IOException(String.Format("Could not find directory '{0}'.", m_Source)) End If LoadSources(True, False) Move() Finally CleanUp() End Try End Sub Sub ExecuteDirDelete() Try Init("Delete") If IO.Directory.Exists(m_Source) = False Then Return End If LoadSources(m_Source, m_DirectoryNotEmpty = DeleteDirectoryOption.DeleteAllContents, False) If m_DirectoryNotEmpty = DeleteDirectoryOption.ThrowIfDirectoryNonEmpty AndAlso m_Sources.Count > 1 Then Throw New IOException("Directory not empty") End If Delete() Finally CleanUp() End Try End Sub Sub ExecuteFileDelete() Try Init("Delete") LoadSources(m_Source, False, False) Delete() Finally CleanUp() End Try End Sub#End Region Private Function GetDestination(ByVal Source As String) As String Dim result As String result = Source.Replace(m_Source, m_Destination) Return result End Function Private Sub Move() Dim counter As Integer Dim size As Long size = 0 If OnSameVolume Then If IsDirectory(m_Source) Then UpdateUI(Path.GetDirectoryName(m_Source), m_Destination, Path.GetFileName(m_Source), 0, 0) System.IO.Directory.Move(m_Source, m_Destination) Else If IO.File.Exists(m_Destination) Then If DoOverwrite(m_Source, m_Destination) Then System.IO.File.Delete(m_Destination) Else m_Cancelled = True Return End If End If System.IO.File.Move(m_Source, m_Destination) End If Else For i As Integer = m_Sources.Count - 1 To 0 Step -1 Dim info As Info = m_Sources(i) Dim item As String = info.Name Dim destination As String destination = GetDestination(item) CopyItem(info, destination, counter, size) If m_Cancelled Then Return DeleteItem(info, counter, False) counter += 1 If m_Cancelled Then Return Next End If UpdateUI(Nothing, Nothing, Nothing, m_Sources.Count, 0) End Sub Private Sub Copy() Dim counter As Integer Dim size As Long size = 0 For i As Integer = 0 To m_Sources.Count - 1 Dim info As Info = m_Sources(i) Dim item As String = info.Name Dim destination As String destination = GetDestination(item) CopyItem(info, destination, counter, size) counter += 1 If m_Cancelled Then Return Next UpdateUI(Nothing, Nothing, Nothing, m_Sources.Count, size) End Sub Private Sub CopyItem(ByVal Info As Info, ByVal Destination As String, ByVal Counter As Integer, ByRef Size As Long) Dim Source As String = Info.Name If Info.IsDir Then UpdateUI(Source, Nothing, Nothing, Counter, 0) If IO.Directory.Exists(Destination) = False Then IO.Directory.CreateDirectory(Destination) End If Else UpdateUI(Path.GetDirectoryName(Source), Path.GetDirectoryName(Destination), Path.GetFileName(Source), Counter, Size) CopyFile(Source, Destination, Size) End If End Sub Private Sub DeleteItem(ByVal Info As Info, ByVal Counter As Integer, ByVal DoUpdate As Boolean) Dim Item As String = Info.Name If Info.IsDir Then If DoUpdate Then UpdateUI(Item, Nothing, Nothing, Counter, 0) System.IO.Directory.Delete(Item, False) Else If DoUpdate Then UpdateUI(Path.GetDirectoryName(Item), Nothing, Path.GetFileName(Item), Counter, 0) System.IO.File.Delete(Item) End If End Sub Private Function IsDirectory(ByVal Path As String) As Boolean Return CBool(System.IO.File.GetAttributes(Path) And FileAttributes.Directory)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -