📄 filesystem.vb
字号:
'' FileSystem.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.ObjectModelNamespace Microsoft.VisualBasic.FileIO Public Class FileSystem Public Sub New() 'Empty End Sub Private Shared Function StripTrailingSlash(ByVal dir As String) As String Return dir.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) End Function Public Shared Function CombinePath(ByVal baseDirectory As String, ByVal relativePath As String) As String Return StripTrailingSlash(Path.Combine(Path.GetFullPath(baseDirectory), relativePath)) End Function Public Shared Sub CopyDirectory(ByVal sourceDirectoryName As String, ByVal destinationDirectoryName As String) CopyDirectory(sourceDirectoryName, destinationDirectoryName, False) End Sub Public Shared Sub CopyDirectory(ByVal sourceDirectoryName As String, ByVal destinationDirectoryName As String, ByVal showUI As UIOption) CopyDirectory(sourceDirectoryName, destinationDirectoryName, showUI, UICancelOption.ThrowException) End Sub Public Shared Sub CopyDirectory(ByVal sourceDirectoryName As String, ByVal destinationDirectoryName As String, ByVal overwrite As Boolean) Dim op As New FileSystemOperation(sourceDirectoryName, destinationDirectoryName, overwrite) op.ExecuteDirCopy() End Sub Public Shared Sub CopyDirectory(ByVal sourceDirectoryName As String, ByVal destinationDirectoryName As String, ByVal showUI As UIOption, ByVal onUserCancel As UICancelOption) Dim op As New FileSystemOperation(sourceDirectoryName, destinationDirectoryName, showUI, onUserCancel) op.ExecuteDirCopy() End Sub Public Shared Sub CopyFile(ByVal sourceFileName As String, ByVal destinationFileName As String) CopyFile(sourceFileName, destinationFileName, False) End Sub Public Shared Sub CopyFile(ByVal sourceFileName As String, ByVal destinationFileName As String, ByVal showUI As UIOption) CopyFile(sourceFileName, destinationFileName, showUI, UICancelOption.ThrowException) End Sub Public Shared Sub CopyFile(ByVal sourceFileName As String, ByVal destinationFileName As String, ByVal overwrite As Boolean) Dim op As New FileSystemOperation(sourceFileName, destinationFileName, overwrite) op.ExecuteFileCopy() End Sub Public Shared Sub CopyFile(ByVal sourceFileName As String, ByVal destinationFileName As String, ByVal showUI As UIOption, ByVal onUserCancel As UICancelOption) Dim op As New FileSystemOperation(sourceFileName, destinationFileName, showUI, onUserCancel) op.ExecuteFileCopy() End Sub Public Shared Sub CreateDirectory(ByVal directory As String) System.IO.Directory.CreateDirectory(directory) End Sub Public Shared Sub DeleteDirectory(ByVal directory As String, ByVal onDirectoryNotEmpty As DeleteDirectoryOption) DeleteDirectory(directory, UIOption.OnlyErrorDialogs, RecycleOption.DeletePermanently, UICancelOption.ThrowException, onDirectoryNotEmpty, False) End Sub Public Shared Sub DeleteDirectory(ByVal directory As String, ByVal showUI As UIOption, ByVal recycle As RecycleOption) DeleteDirectory(directory, showUI, recycle, UICancelOption.ThrowException, DeleteDirectoryOption.DeleteAllContents, True) End Sub Public Shared Sub DeleteDirectory(ByVal directory As String, ByVal showUI As UIOption, ByVal recycle As RecycleOption, ByVal onUserCancel As UICancelOption) DeleteDirectory(directory, showUI, recycle, onUserCancel, DeleteDirectoryOption.DeleteAllContents, True) End Sub Private Shared Sub DeleteDirectory(ByVal directory As String, ByVal showUIOption As UIOption, ByVal recycle As RecycleOption, ByVal onUserCancel As UICancelOption, ByVal onDirectoryNotEmpty As DeleteDirectoryOption, ByVal showUI As Boolean) Dim op As New FileSystemOperation(directory, showUIOption, recycle, onUserCancel, onDirectoryNotEmpty, showUI) op.ExecuteDirDelete() End Sub Public Shared Sub DeleteFile(ByVal file As String) DeleteFile(file, UIOption.AllDialogs, RecycleOption.DeletePermanently, UICancelOption.DoNothing, False) End Sub Public Shared Sub DeleteFile(ByVal file As String, ByVal showUI As UIOption, ByVal recycle As RecycleOption) DeleteFile(file, showUI, recycle, UICancelOption.ThrowException, True) End Sub Public Shared Sub DeleteFile(ByVal file As String, ByVal showUI As UIOption, ByVal recycle As RecycleOption, ByVal onUserCancel As UICancelOption) DeleteFile(file, showUI, recycle, onUserCancel, True) End Sub Private Shared Sub DeleteFile(ByVal file As String, ByVal showUIOption As UIOption, ByVal recycle As RecycleOption, ByVal onUserCancel As UICancelOption, ByVal showUI As Boolean) Dim op As New FileSystemOperation(file, showUIOption, recycle, onUserCancel, showUI) op.ExecuteFileDelete() End Sub Public Shared Function DirectoryExists(ByVal directory As String) As Boolean Return IO.Directory.Exists(directory) End Function Public Shared Function FileExists(ByVal file As String) As Boolean Return IO.File.Exists(file) End Function Public Shared Function FindInFiles(ByVal directory As String, ByVal containsText As String, ByVal ignoreCase As Boolean, ByVal searchType As SearchOption) As ReadOnlyCollection(Of String) Return FindInFiles(directory, containsText, ignoreCase, searchType, Nothing) End Function Public Shared Function FindInFiles(ByVal directory As String, ByVal containsText As String, ByVal ignoreCase As Boolean, ByVal searchType As SearchOption, ByVal ParamArray fileWildcards As String()) As ReadOnlyCollection(Of String) Dim list As New Generic.List(Of String) FindInFiles2(directory, containsText, ignoreCase, searchType, fileWildcards, list) Return New ReadOnlyCollection(Of String)(list) End Function Private Shared Sub FindInFiles2(ByVal directory As String, ByVal containsText As String, ByVal ignoreCase As Boolean, ByVal searchType As SearchOption, ByVal fileWildcards() As String, ByVal result As Generic.List(Of String)) Dim files As New Generic.List(Of String) If fileWildcards Is Nothing OrElse fileWildcards.Length = 0 Then files.AddRange(System.IO.Directory.GetFiles(directory)) Else For Each wildcard As String In fileWildcards AddUniqueToList(files, System.IO.Directory.GetFiles(directory, wildcard)) Next End If If containsText <> "" Then For Each file As String In files If FileContainsText(file, containsText, ignoreCase) Then result.Add(file) End If Next Else result.AddRange(files) End If If searchType = SearchOption.SearchAllSubDirectories Then Dim subdirs() As String subdirs = System.IO.Directory.GetDirectories(directory) For Each subdir As String In subdirs FindInFiles2(subdir, containsText, ignoreCase, searchType, fileWildcards, result) Next End If End Sub Private Shared Sub AddUniqueToList(ByVal list As Generic.List(Of String), ByVal items As String()) For Each item As String In items If list.Contains(item) = False Then list.Add(item) Next End Sub Private Shared Function FileContainsText(ByVal file As String, ByVal text As String, ByVal ignoreCase As Boolean) As Boolean Dim currentChar As Char Dim peekedChar As Generic.Queue(Of Char) If text Is Nothing OrElse text = String.Empty Then Return True peekedChar = New Generic.Queue(Of Char) If ignoreCase Then text = text.ToUpperInvariant()#If TARGET_JVM = False Then 'FileStream ctor with FileOptions Not Supported by Grasshopper Using reader As New IO.StreamReader(New IO.FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 1024, FileOptions.SequentialScan))#Else Using reader As New IO.StreamReader(New IO.FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 1024))#End If currentChar = Microsoft.VisualBasic.ChrW(reader.Read) Do Until currentChar = Char.MinValue If (ignoreCase AndAlso Char.ToUpperInvariant(currentChar) = text(0)) OrElse (currentChar = text(0)) Then If text.Length = 1 Then Return True Dim noMatch As Boolean Dim tmp As Char() = peekedChar.ToArray noMatch = False For i As Integer = 0 To tmp.Length - 1 If text.Length = i + 2 Then Return True If text(i + 1) <> tmp(i) Then noMatch = True Exit For End If Next If noMatch = False Then If text.Length = tmp.Length + 1 Then Return True For i As Integer = tmp.Length To text.Length - 2 If reader.EndOfStream Then Return False Dim tmpChar As Char = Microsoft.VisualBasic.ChrW(reader.Read) If tmpChar = Char.MinValue Then Return False peekedChar.Enqueue(tmpChar) If Not ((ignoreCase AndAlso Char.ToUpperInvariant(tmpChar) = text(i + 1)) OrElse (tmpChar = text(i + 1))) Then noMatch = True Exit For End If Next If noMatch = False Then Return True End If End If
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -