我有一个非常简短的问题。我的程序实际上下载了一个 zip 文件,然后将其解压到桌面上。但我需要它的卸载功能,这基本上是删除多个文件夹和包含文件。我怎样才能在 vb.net 中做到这一点?
如果所有文件夹都包含在一个文件夹中,那么它应该非常简单。
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\YOURPATH"
System.IO.Directory.Delete(path, True)
这将删除您的根目录及其下面的所有目录和文件。 如果您的文件和目录不全部位于单个根目录(如示例中的“YOURPATH”)中,您可以多次调用此命令。 这将使您不必单独删除每个文件。
.NET IO 单元有两个命令可以让您完成此任务:
System.IO.Directory.GetDirectories("C:\\Program Files\\Your Directory\\*.*");
System.IO.Directory.GetFiles("C:\\Program Files\\Your Directory\\*.*");
我会编写一个方法,该方法采用目录名称并使用“GetFiles”例程来获取所有文件,并在 foreach 循环中使用 System.IO.File.Delete(path) 删除它们。 然后,对 GetDirectories() 命令的结果运行 foreach 循环,递归调用该函数。
更新:Steve Danner 指出 System.IO.Directory 命名空间有一个删除方法,因此您不需要经历我在这里讨论的循环。 他的答案是正确的,应该投赞成票。我的,在这一点上,更多的是好奇(尽管感谢给我投赞成票的人;0)。
您正在寻找 DirectoryInfo,请像这样使用它:
Dim di As New IO.DirectoryInfo(path)
di.Delete(True)
这是我如何使用“Select Case statements”和“Try statements”从选定目录中递归删除文件和/或文件夹和文件以捕获大多数全局错误事件的两个示例。这个程序不是开玩笑,所以请确保您删除了您想要处理的内容。
Private Sub EternalFlushInitiate(sender As Object, e As EventArgs) Handles MyBase.Load
Dim case_Value As Integer = 3
Select Case case_Value
Case 1 To 3
'Try Statements provides a way to handle some or all possible errors that may occur in a given block of code,
'while still running code.
Try
SetAttributesCleenSweep(My.Computer.FileSystem.SpecialDirectories.MyMusic)
My.Computer.FileSystem.DeleteDirectory(My.Computer.FileSystem.SpecialDirectories.MyMusic, FileIO.DeleteDirectoryOption.DeleteAllContents)
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Try
SetAttributesCleenSweep(My.Computer.FileSystem.SpecialDirectories.MyPictures)
My.Computer.FileSystem.DeleteDirectory(My.Computer.FileSystem.SpecialDirectories.MyPictures, FileIO.DeleteDirectoryOption.DeleteAllContents)
Catch ex As Exception
End Try
Try
SetAttributesCleenSweep(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
My.Computer.FileSystem.DeleteDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments, FileIO.DeleteDirectoryOption.DeleteAllContents)
Catch ex As Exception
End Try
End Select
End Sub
Private Sub SetAttributesCleenSweep(assistdirectory As String)
For Each fileName As String In My.Computer.FileSystem.GetFiles(assistdirectory)
Try
'set the file attributes to ensure that we can delete the file
My.Computer.FileSystem.GetFileInfo(fileName).Attributes = FileAttributes.Normal
Catch ex As Exception
Debug.WriteLine("Could not set attributes on file: " + fileName)
End Try
Next
For Each target As String In My.Computer.FileSystem.GetDirectories(assistdirectory)
Try
'set the file attributes to ensure that we can delete the directory
My.Computer.FileSystem.GetFileInfo(target).Attributes = FileAttributes.Directory
Catch ex As Exception
Debug.WriteLine("Could not set attributes on directory: " + target)
End Try
'Recursive-Method: All files & folders deleted
SetAttributesCleenSweep(target)
Next
End Sub
Second Method: Shell | Delete Files
Imports System.IO
Public Class Explorer
Private Sub Explorer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Scope() 'Key
End Sub
Private Sub Scope()
REM: Wrappers that compliment the "NativeMethods Class"
Dim path = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Contacts)
Dim path1 = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Downloads)
Dim path2 = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Links)
Dim path3 = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Music)
Dim path4 = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Pictures)
Dim path5 = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.SavedGames)
Dim path6 = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.SavedSearches)
Dim path7 = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Videos)
REM: End of Wrappers
'Directory.GetFiles: Returns the names of files (including their paths) in the specified directory.
For Each filename As String In Directory.GetFiles(path3) 'Native Music
File.Delete(filename)
Next
For Each filename1 As String In Directory.GetFiles(path4) 'Native Pictures
File.Delete(filename1)
Next
For Each filename2 As String In Directory.GetFiles(path7) 'Native Videos
File.Delete(filename2)
Next
'Continuance....For Each "NativeMethods Path"
End Sub
End Class
Now you create a "NativeMethods Class"
Imports System.Runtime.InteropServices
Partial Public Class NativeMethods
'An access token that represents a particular user. If this parameter is NULL, which is the most common usage,
'the function requests the known folder for the current user.
'Request a specific user's folder by passing the hToken of that user.
'This is typically done in the context of a service that has sufficient privileges to retrieve the token of a given user.
'That token must be opened with TOKEN_QUERY and TOKEN_IMPERSONATE rights.
'In some cases, you also need to include TOKEN_DUPLICATE. In addition to passing the user's hToken,
'the registry hive of that specific user must be mounted. See Access Control for further discussion of access control issues.
'Assigning the hToken parameter a value Of -1 indicates the Default User.
'This allows clients Of SHGetKnownFolderPath To find folder locations (such As the Desktop folder) For the Default User.
'The Default User user profile Is duplicated When any New user account Is created,
'And includes special folders such As Documents And Desktop.
'Any items added To the Default User folder also appear In any New user account.
'Note that access To the Default User folders requires administrator privileges.
<DllImport("shell32.dll")>
Private Shared Function SHGetKnownFolderPath(<MarshalAs(UnmanagedType.LPStruct)>
rfid As Guid,
dwFlags As UInteger,
hToken As IntPtr,
ByRef pszPath As IntPtr) As Integer
End Function
'The system special folders are folders such As Program Files, Programs, System, Or Startup, which contain common information.
'Special folders are Set by Default by the system, Or explicitly by the user, When installing a version Of Windows.
'The Environment.GetFolderPath method returns the locations associated With this enumeration.
'The locations Of these folders can have different values On different operating systems,
'The user can change some Of the locations, And the locations are localized.
'For more information about special folders, see the KNOWNFOLDERID constants in the Windows documentation.
Public Enum ShellSpecialFolders 'In order: see above
Contacts
Downloads
Links
Music
Pictures
SavedGames
SavedSearches
Videos
End Enum
Private Shared ReadOnly ShellFolderGuids As Guid() = {
Guid.Parse("{56784854-C6CB-462B-8169-88E350ACB882}"), 'Contacts
Guid.Parse("{374DE290-123F-4565-9164-39C4925E467B}"), 'Downloads
Guid.Parse("{BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968}"), 'Links
Guid.Parse("{4BD8D571-6D19-48D3-BE97-422220080E43}"), 'Music
Guid.Parse("{33E28130-4E1E-4676-835A-98395C3BC3BB}"), 'Pictures
Guid.Parse("{4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4}"), 'SavedGames
Guid.Parse("{7D1D3A04-DEBB-4115-95CF-2F29DA2920DA}"), 'SavedSearches
Guid.Parse("{18989B1D-99B5-455B-841C-AB7C74E4DDFC}") 'Videos
}
Friend Shared Function GetSpecialFolder(folder As ShellSpecialFolders) As String
Dim ret As Integer
Dim fPath As IntPtr
' == "Dont Vertify" flag:
Dim SHFlag As UInteger = &H4000
ret = SHGetKnownFolderPath(ShellFolderGuids(folder), SHFlag,
New IntPtr(0), fPath)
If ret = 0 Then
Return Marshal.PtrToStringUni(fPath)
Else
Return ""
End If
End Function
' Optional single purpose version
Friend Shared Function GetSpecialVideoFolder() As String
Return GetSpecialFolder(ShellSpecialFolders.Videos)
End Function
End Class
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\YOURPATH"
System.IO.Directory.Delete(path, True)