📄 filesystemoperation.vb
字号:
End Function Private Sub CopyFile(ByVal Source As String, ByVal Destination As String, ByRef Size As Long) Dim newFileMode As FileMode If IO.File.Exists(Destination) Then If DoOverwrite(Source, Destination) = False Then Return newFileMode = FileMode.Create Else newFileMode = FileMode.CreateNew End If Try#If TARGET_JVM = False Then 'FileStream ctor with FileOptions Not Supported by Grasshopper Using reader As New IO.FileStream(Source, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, FileOptions.SequentialScan) Using writer As New IO.FileStream(Destination, newFileMode, FileAccess.Write, FileShare.Read, 1024, FileOptions.SequentialScan)#Else Using reader As New IO.FileStream(Source, FileMode.Open, FileAccess.Read, FileShare.Read, 1024) Using writer As New IO.FileStream(Destination, newFileMode, FileAccess.Write, FileShare.Read)#End If Dim read As Integer Dim buffer(1023) As Byte Do read = reader.Read(buffer, 0, 1024) writer.Write(buffer, 0, read) Size = Size + CLng(read) UpdateUI(Size) Loop Until read = 0 OrElse m_Cancelled End Using End Using Catch ex As IOException m_Errors.Add(Source, ex.Message) End Try End Sub Private Sub Delete() Dim counter As Integer If m_Recycle = RecycleOption.SendToRecycleBin Then Throw New NotImplementedException 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 DeleteItem(info, counter, True) counter += 1 If m_Cancelled Then Return Next End If UpdateUI(Nothing, Nothing, Nothing, m_Sources.Count, 0) End Sub Private Sub Recycle(ByVal Source As String) End Sub Private Sub LoadSources(ByVal Recursive As Boolean, ByVal SizeMatters As Boolean) m_TotalSize = 0 LoadSources(m_Source, Recursive, SizeMatters, 0) End Sub Private Sub LoadSources(ByVal Source As String, ByVal Recursive As Boolean, ByVal SizeMatters As Boolean, Optional ByRef DirSize As Long = 0) Dim subdirs() As String Dim files() As String Dim info2 As Info = Nothing Dim subsize As Long If CBool(System.IO.File.GetAttributes(Source) And FileAttributes.Directory) Then info2 = New Info info2.Name = Source info2.IsDir = True m_Sources.Add(info2) Debug.WriteLine(info2.Name) files = System.IO.Directory.GetFiles(Source) For Each file As String In files Dim info As New Info info.Name = file info.IsDir = False If SizeMatters Then info.Size = (New FileInfo(file)).Length m_TotalSize += info.Size DirSize += info.Size End If m_Sources.Add(info) Debug.WriteLine(info.Name) Next If Recursive Then subdirs = System.IO.Directory.GetDirectories(Source) For Each subdir As String In subdirs LoadSources(subdir, Recursive, SizeMatters, subsize) Next End If If info2 IsNot Nothing Then info2.Size = subsize DirSize += subsize End If Else Dim info As New Info info.Name = Source m_Sources.Add(info) If SizeMatters Then info.Size = (New FileInfo(Source)).Length m_TotalSize = info.Size End If End If End Sub Private Sub Init(ByVal Title As String) m_Source = Path.GetFullPath(m_Source) If m_Destination <> String.Empty Then m_Destination = Path.GetFullPath(m_Destination) End If If m_ShowUI = False Then Return#If TARGET_JVM = False Then 'Windows.Forms Not Supported by Grasshopper m_UI = New FileSystemOperationUI(Me) m_UI.Text = Title m_UI.lblDirs.Text = "Calculating time..." m_UI.lblFile.Text = String.Empty m_UI.lblTimeLeft.Text = "..." m_UI.barProgress.Value = 0 m_UI.Show()#End If End Sub Private Sub UpdateUI(ByVal SizeDone As Long) If m_ShowUI = False Then Return Dim PercentDone As Double If SizeDone > 0 AndAlso m_TotalSize > 0 Then PercentDone = SizeDone / m_TotalSize * 100 Else PercentDone = 0 End If#If TARGET_JVM = False Then 'Windows.Forms Not Supported by Grasshopper m_UI.UpdateInfo(PercentDone)#End If End Sub Private Sub UpdateUI(ByVal SourceDirectory As String, ByVal DestinationDirectory As String, ByVal File As String, ByVal ItemsDone As Integer, ByVal SizeDone As Long) If m_ShowUI = False Then Return Dim PercentDone As Double If SizeDone > 0 AndAlso m_TotalSize > 0 Then PercentDone = SizeDone / m_TotalSize * 100 ElseIf ItemsDone > 0 AndAlso m_Sources.Count > 0 Then PercentDone = ItemsDone / m_Sources.Count Else PercentDone = 0 End If#If TARGET_JVM = False Then 'Windows.Forms Not Supported by Grasshopper m_UI.UpdateInfo(SourceDirectory, DestinationDirectory, File, PercentDone)#End If End Sub Private Sub CleanUp() If m_ShowUI Then#If TARGET_JVM = False Then 'Windows.Forms Not Supported by Grasshopper If m_UI IsNot Nothing Then m_UI.Dispose() m_UI = Nothing#End If If m_Cancelled AndAlso m_UICancelOption = UICancelOption.ThrowException Then Throw New OperationCanceledException("The operation was canceled.") End If End If If m_Errors.Count > 0 Then If CBool(File.GetAttributes(m_Source) And FileAttributes.Directory) = False Then Throw New IOException(m_Errors(m_Source)) Else Dim ex As New IOException("Could not complete operation on some files and directories. See the Data property of the exception for more details.") For Each entry As KeyValuePair(Of String, String) In m_Errors ex.Data.Add(entry.Key, entry.Value) Next Throw ex End If End If End Sub Private Sub CopyDir(ByVal SourceDir As String, ByVal DestinationDir As String) If IO.Directory.Exists(DestinationDir) = False Then IO.Directory.CreateDirectory(DestinationDir) End If Dim files() As String = IO.Directory.GetFiles(SourceDir) Dim subdirs() As String = IO.Directory.GetDirectories(SourceDir) For Each file As String In files System.IO.File.Copy(file, Path.Combine(DestinationDir, Path.GetFileName(file))) Next For Each subdir As String In subdirs Dim name As String = Path.GetFileName(subdir) CopyDir(Path.Combine(SourceDir, name), Path.Combine(DestinationDir, name)) Next End Sub Private Function DoOverwrite(ByVal Source As String, ByVal Destination As String) As Boolean If m_ShowUI Then Static overWriteAll As Boolean Static overWriteNone As Boolean If overWriteAll Then Return True If overWriteNone Then Return False If m_ShowUIOption = UIOption.OnlyErrorDialogs Then Return True#If TARGET_JVM = False Then 'Windows.Forms Not Supported by Grasshopper Using frm As New FileSystemOperationUIQuestion Dim result As FileSystemOperationUIQuestion.Answer Dim infoA As FileInfo = New FileInfo(Source) Dim infoB As FileInfo = New FileInfo(Destination) frm.Text = "Confirm file overwrite" frm.lblTitle.Text = String.Format("This folder already has a file called '{0}'.", Path.GetFileName(Source)) frm.lblText1.Text = "Do you want to replace the existing file" frm.lblText2.Text = "with this other file?" frm.lblSizeA.Text = String.Format("{0} bytes", infoA.Length) frm.lblSizeB.Text = String.Format("{0} bytes", infoB.Length) frm.lblDateA.Text = String.Format("modified: {0}", infoA.LastWriteTime) frm.lblDateB.Text = String.Format("modified: {0}", infoB.LastWriteTime) frm.iconA.Image = System.Drawing.Icon.ExtractAssociatedIcon(Source).ToBitmap frm.iconB.Image = System.Drawing.Icon.ExtractAssociatedIcon(Destination).ToBitmap result = frm.ShowDialog() Select Case result Case FileSystemOperationUIQuestion.Answer.Cancel m_Cancelled = True Return False Case FileSystemOperationUIQuestion.Answer.No Return False Case FileSystemOperationUIQuestion.Answer.NoToAll overWriteNone = True Return False Case FileSystemOperationUIQuestion.Answer.Yes Return True Case FileSystemOperationUIQuestion.Answer.YesToAll overWriteAll = True Return True Case Else Return False End Select End Using#End If Else If m_Overwrite = False Then m_Errors.Add(Source, String.Format("The file '{0}' already exists.", Destination)) End If Return m_Overwrite End If End Function Private ReadOnly Property OnSameVolume() As Boolean Get Return IsOnSameVolume(m_Source, m_Destination) End Get End Property Private Shared Function IsOnSameVolume(ByVal Source As String, ByVal Destination As String) As Boolean Return Char.ToUpperInvariant(Source(0)) = Char.ToUpperInvariant(Destination(0)) End Function End ClassEnd Namespace#End If
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -