Added more stuff
This commit is contained in:
51
shared/trashcan.go
Normal file
51
shared/trashcan.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Constants for the SHFileOperation function
|
||||
const (
|
||||
FO_DELETE = 3
|
||||
FOF_ALLOWUNDO = 0x0040 // Move to Recycle Bin instead of permanently deleting
|
||||
FOF_NOCONFIRMATION = 0x0010 // No confirmation dialogs
|
||||
)
|
||||
|
||||
// SHFILEOPSTRUCT structure (Windows API)
|
||||
type SHFILEOPSTRUCT struct {
|
||||
Hwnd uintptr
|
||||
WFunc uint32
|
||||
PFrom *uint16
|
||||
PTo *uint16
|
||||
FFlags uint16
|
||||
FAnyOperationsAborted bool
|
||||
HNameMappings uintptr
|
||||
LpszProgressTitle *uint16
|
||||
}
|
||||
|
||||
// MoveToTrash moves a file or folder to the Windows Recycle Bin.
|
||||
func MoveToTrash(path string) error {
|
||||
// The string must be double null-terminated for the Windows API
|
||||
from, err := syscall.UTF16FromString(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
op := SHFILEOPSTRUCT{
|
||||
WFunc: FO_DELETE,
|
||||
PFrom: &from[0],
|
||||
FFlags: FOF_ALLOWUNDO | FOF_NOCONFIRMATION,
|
||||
}
|
||||
|
||||
shell32 := syscall.NewLazyDLL("shell32.dll")
|
||||
shFileOperation := shell32.NewProc("SHFileOperationW")
|
||||
|
||||
ret, _, _ := shFileOperation.Call(uintptr(unsafe.Pointer(&op)))
|
||||
if ret != 0 {
|
||||
return fmt.Errorf("failed to move to trash: error code %d", ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user