<System.Security.SuppressUnmanagedCodeSecurity()>
Friend NotInheritable Class NativeMethods
Inherits Attribute
Private Sub New()
End Sub
''' <summary>
''' Copia un bloque de memoria de un lugar a otro.
''' https://msdn.microsoft.com/es-es/library/windows/desktop/aa366535(v=vs.85).aspx
''' </summary>
''' <param name="Destination">Un puntero a la dirección inicial del destino del bloque copiado.</param>
''' <param name="Source">Un puntero a la dirección inicial del bloque de memoria para copiar.</param>
''' <param name="Length">El tamaño del bloque de memoria para copiar, en bytes.</param>
''' <remarks></remarks>
<DllImport("KERNEL32", EntryPoint:="RtlMoveMemory")> _
Private Shared Sub CopyMemory(<MarshalAs(UnmanagedType.I1)> ByRef Destination As Byte,
<MarshalAs(UnmanagedType.AsAny)> ByVal Source As Object, ByVal Length As Integer)
End Sub
'SHGetPathFromIDListW (En VB se usa este entrada)
''' <summary>
'''Copia, mueve, cambia el nombre o elimina un objeto de sistema de archivos. Esta función se ha reemplazado en Windows Vista por IFileOperation.
'''https://msdn.microsoft.com/en-us/library/windows/desktop/bb762164(v=vs.85).aspx
''' </summary>
''' <param name="lpFileOp"></param>
''' <returns></returns>
''' <remarks></remarks>
<DllImport("shell32.dll", EntryPoint:="SHFileOperationA")> _
Public Shared Function SHFileOperation(<[In], [Out](), MarshalAs(UnmanagedType.Struct)> ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
End Function
' Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure SHFILEOPSTRUCT
Public hWnd As IntPtr
Public wFunc As Integer
<MarshalAs(UnmanagedType.LPStr)> Public pFrom As String
<MarshalAs(UnmanagedType.LPStr)> Public pTo As String
Public fFlags As UShort
<MarshalAs(UnmanagedType.Bool)> Public fAnyOperationsAborted As Boolean
Public hNameMappings As IntPtr
<MarshalAs(UnmanagedType.LPStr)> Public lpszProgressTitle As String
End Structure
Private Const FOF_CONFIRMMOUSE As Short = &H2S
Private Const FOF_ALLOWUNDO As Short = &H40S 'Envía a la papelera de reciclaje
Private Const FOF_FILESONLY As Short = &H80S
Private Const FOF_MULTIDESTFILES As Short = &H1S
Private Const FOF_NOCONFIRMATION As Short = &H10S ' No pedir confirmación al usuario.
Private Const FOF_NOCONFIRMMKDIR As Short = &H200S
Private Const FOF_NO_CONNECTED_ELEMENTS As Short = &H1000S
Private Const FOF_NOCOPYSECURITYATTRIBS As Short = &H800S
Private Const FOF_NOERRORUI As Short = &H400S
Private Const FOF_RENAMEONCOLLISION As Short = &H8S
Private Const FOF_SILENT As Short = &H4S ' no crear avance/informe
Private Const FOF_SIMPLEPROGRESS As Short = &H100S
Private Const FOF_WANTMAPPINGHANDLE As Short = &H20S
Private Const FOF_WANTNUKEWARNING As Short = &H2000S
Private Const FOF_NORECURSION As Short = &H1000S '// don't recurse into directories.
Private Const FO_DELETE As Short = &H3
''' <summary>
''' Función para borrar archivos
''' </summary>
''' <param name="Ounerhwnd"></param>
''' <param name="sFile">Archivo o carpeta a eliminar</param>
''' <param name="RecycleMode">boleano: True-> envía a la papelera de reciclaje, False ->no envía a la papelera</param>
''' <param name="DialogConfirmation">Boleano: True-> muestra un diálogo, False-> no muestra diálogo</param>
''' <remarks></remarks>
Public Shared Sub SHfileOpDelete(ByVal Ounerhwnd As IntPtr, ByVal sFile As String, ByVal RecycleMode As Boolean, ByVal DialogConfirmation As Boolean)
Dim Result As Integer = Nothing
Dim lenFileop As Integer
Dim foBuf() As Byte
Dim SHFileOp As New SHFILEOPSTRUCT
lenFileop = Marshal.SizeOf(SHFileOp) ' double word alignment increase
ReDim Preserve foBuf(CInt(lenFileop)) ' the size of the structure.
With SHFileOp
.hWnd = Ounerhwnd
.wFunc = FO_DELETE
.pFrom = sFile & vbNullChar & vbNullChar
.hNameMappings = IntPtr.Zero
If RecycleMode Then
If DialogConfirmation Then
.fFlags = FOF_ALLOWUNDO 'Envía a la papelera de reciclaje con diálogo de confirmación
Else
.fFlags = FOF_ALLOWUNDO Or FOF_NOCONFIRMATION 'Envía a la papelera de reciclaje sin mensaje de confirmación
End If
Else
If DialogConfirmation Then
.fFlags = FOF_SILENT 'Elimina diréctamente con diálogo de confirmación
Else
.fFlags = FOF_NOCONFIRMATION Or FOF_SILENT 'Elimina directamente sin diálogo de confirmación
End If
End If
End With
CopyMemory(foBuf(1), SHFileOp, lenFileop)
CopyMemory(foBuf(19), foBuf(21), 12)
Try
Result = SHFileOperation(SHFileOp)
Catch ex As Exception
MessageBox.
Show(Err.
Description) End Try
End Sub
End Class