C++ Go语言实现将windows和linux文件删除至回收站

C++

在C++中,将文件移动到回收站的实现在Linux和Windows平台上是不同的。首先,我会为你提供在Windows平台上实现的代码示例,然后再提供Linux平台上的对应实现。

Windows平台

在Windows平台上,你可以使用SHFileOperation函数来将文件移动到回收站。这个函数定义在Shellapi.h头文件中。以下是一个简单的示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <windows.h>
#include <shellapi.h>
 
void moveToRecycleBin(const char* filePath) {
    SHFILEOPSTRUCT fileOp;
    ZeroMemory(&fileOp, sizeof(fileOp));
    fileOp.wFunc = FO_DELETE;
    fileOp.pFrom = filePath;
    fileOp.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
 
    SHFileOperation(&fileOp);
}
 
int main() {
    moveToRecycleBin("C:\\path\\to\\your\\file.txt");
    return 0;
}

Linux平台

在Linux系统中,将文件“删除”到回收站的操作实际上并不是直接删除文件,而是将其移动到一个特定的目录(通常是用户目录下的一个隐藏文件夹)。这是因为Linux没有一个统一的、系统级的回收站功能,不像Windows的回收站那样。因此,将文件“删除”到回收站实际上是把文件从它原来的位置移动到这个隐藏的回收站目录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
 
bool moveToTrash(const std::string& filePath) {
    const char* homeDir = getenv("HOME");
    if (!homeDir) {
        std::cerr << "Error: HOME environment variable not set." << std::endl;
        return false;
    }
 
    std::string trashFilesPath = std::string(homeDir) + "/.local/share/Trash/files/";
    std::string trashInfoPath = std::string(homeDir) + "/.local/share/Trash/info/";
    std::string baseName = filePath.substr(filePath.find_last_of('/') + 1);
    std::string destFilePath = trashFilesPath + baseName;
    std::string destInfoPath = trashInfoPath + baseName + ".trashinfo";
 
    // 创建回收站文件和信息目录(如果不存在)
    mkdir(trashFilesPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
    mkdir(trashInfoPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
 
    // 写入.trashinfo文件
    std::ofstream trashInfoFile(destInfoPath);
    if (!trashInfoFile) {
        std::cerr << "Error: Unable to create trash info file." << std::endl;
        return false;
    }
    trashInfoFile << "[Trash Info]\nPath=" << filePath << "\nDeletionDate="
                  << /* 获取当前日期和时间的代码 */ "\n";
    trashInfoFile.close();
 
    // 移动文件到回收站
    if (rename(filePath.c_str(), destFilePath.c_str()) != 0) {
        perror("Error moving file to trash");
        return false;
    }
 
    return true;
}
 
int main() {
    std::string filePath = "/path/to/your/file.txt"; // 替换为要删除的文件路径
    if (!moveToTrash(filePath)) {
        return 1;
    }
 
    return 0;
}

跨平台,代码合并

要在同一个程序中同时支持Windows和Linux平台的文件删除到回收站的功能,我们可以使用预处理器指令来区分操作系统,并在每个平台上执行相应的操作。同时,为了确保代码的稳定性,我们需要添加适当的错误处理来避免程序崩溃即使在删除失败的情况下。以下是一个跨平台的示例实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
 
#ifdef _WIN32
#include <windows.h>
#include <shellapi.h>
#endif
 
#ifdef __linux__
#include <sys/stat.h>
#include <unistd.h>
#endif
 
bool moveToRecycleBin(const std::string& filePath) {
    #ifdef _WIN32
    // Windows 实现
    SHFILEOPSTRUCT shfos;
    ZeroMemory(&shfos, sizeof(shfos));
    shfos.wFunc = FO_DELETE;
    shfos.pFrom = filePath.c_str();
    shfos.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
 
    return SHFileOperation(&shfos) == 0;
 
    #elif __linux__
    // Linux 实现
    const char* homeDir = getenv("HOME");
    if (!homeDir) {
        std::cerr << "Error: HOME environment variable not set." << std::endl;
        return false;
    }
 
    std::string trashFilesPath = std::string(homeDir) + "/.local/share/Trash/files/";
    std::string trashInfoPath = std::string(homeDir) + "/.local/share/Trash/info/";
    std::string baseName = filePath.substr(filePath.find_last_of('/') + 1);
    std::string destFilePath = trashFilesPath + baseName;
    std::string destInfoPath = trashInfoPath + baseName + ".trashinfo";
 
    mkdir(trashFilesPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
    mkdir(trashInfoPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
 
    std::ofstream trashInfoFile(destInfoPath);
    if (!trashInfoFile) {
        std::cerr << "Error: Unable to create trash info file." << std::endl;
        return false;
    }
    trashInfoFile << "[Trash Info]\nPath=" << filePath << "\nDeletionDate="
                  << /* 获取当前日期和时间的代码 */ "\n";
    trashInfoFile.close();
 
    return rename(filePath.c_str(), destFilePath.c_str()) == 0;
 
    #else
    std::cerr << "Unsupported platform." << std::endl;
    return false;
    #endif
}
 
int main() {
    std::string filePath = "/path/to/your/file.txt"; // 替换为要删除的文件路径
    if (!moveToRecycleBin(filePath)) {
        std::cerr << "Failed to move file to recycle bin." << std::endl;
        return 1;
    }
    return 0;
}

Go

在Go语言中,将文件移动到回收站的功能比较复杂,因为Go本身没有提供直接操作系统回收站的API。这意味着你需要调用操作系统特定的功能。对于Windows,你可以使用系统调用来调用相应的Windows API。而在Linux上,由于标准的“回收站”是桌面环境特定的,通常的做法是将文件移动到一个特定的目录(例如,基于FreeDesktop.org规范的“回收站”目录)。

实现步骤

平台检测:首先,你需要检测运行程序的平台,以便确定使用哪种方法。

Windows实现:在Windows上,你可以使用syscall包来调用SHFileOperation函数,它是Windows API的一部分,用于执行文件操作,包括删除到回收站。

Linux实现:在Linux上,你可以简单地将文件移动到特定的回收站目录(通常是~/.local/share/Trash/files/),但这不是标准化的,可能会根据不同的桌面环境有所变化。

Go语言实现示例

以下是一个简化的Go语言实现示例。请注意,这个示例仅适用于演示目的,并不包括详细的错误处理和复杂的操作系统交互。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
 
import (
    "fmt"
    "os"
    "path/filepath"
    "runtime"
    "syscall"
    "unsafe"
)
 
// Windows API常量
const (
    FO_DELETE           = 0x0003
    FOF_ALLOWUNDO       = 0x0040
    FOF_NOCONFIRMATION  = 0x0010
)
 
type SHFILEOPSTRUCT struct {
    hwnd    syscall.Handle
    wFunc   uint32
    pFrom   *uint16
    pTo     *uint16
    fFlags  uint16
    fAnyOps bool
    hNameMappings uintptr
    lpszProgressTitle *uint16
}
 
func moveToRecycleBin(filePath string) error {
    switch runtime.GOOS {
    case "windows":
        // Windows实现
        shFileOp := &SHFILEOPSTRUCT{
            wFunc:  FO_DELETE,
            pFrom:  syscall.StringToUTF16Ptr(filePath + "\x00"),
            fFlags: FOF_ALLOWUNDO | FOF_NOCONFIRMATION,
        }
 
        shfileopProc, err := syscall.LoadDLL("shell32.dll").FindProc("SHFileOperationW")
        if err != nil {
            return err
        }
 
        ret, _, _ := shfileopProc.Call(uintptr(unsafe.Pointer(shFileOp)))
        if ret != 0 {
            return fmt.Errorf("SHFileOperationW failed: return value %d", ret)
        }
 
    case "linux":
        // Linux实现
        homeDir, err := os.UserHomeDir()
        if err != nil {
            return err
        }
 
        trashPath := filepath.Join(homeDir, ".local/share/Trash/files")
        if _, err := os.Stat(trashPath); os.IsNotExist(err) {
            if err := os.MkdirAll(trashPath, 0755); err != nil {
                return err
            }
        }
 
        baseName := filepath.Base(filePath)
        destPath := filepath.Join(trashPath, baseName)
        err = os.Rename(filePath, destPath)
        if err != nil {
            return err
        }
 
    default:
        return fmt.Errorf("unsupported platform")
    }
 
    return nil
}
 
func main() {
    err := moveToRecycleBin("C:\\path\\to\\your\\file.txt") // 替换为你要删除的文件路径
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(1)
    }
}

平台检测:通过runtime.GOOS检测运行的操作系统。

Windows实现:(注释掉的部分)需要使用syscall包来调用Windows API。这是一个比较高级和复杂的操作,需要对Windows API有深入了解。

Linux实现:简单地将文件移动到预定义的回收站目录。

错误处理:在实际应用中,应该添加更多的错误处理逻辑以处理各种可能的异常情况。

go单独的windows版本实现

在Go语言中实现将文件移动到Windows回收站的功能相对复杂,因为需要使用Windows API。这通常涉及到调用SHFileOperation函数。在Go中,你可以通过syscall包来进行系统调用。以下是一个可能的实现方式,但请注意,这需要对Windows API有一定的了解,并且可能需要根据你的具体需求进行调整。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
 
import (
    "fmt"
    "os"
    "syscall"
    "unsafe"
)
 
const (
    FO_DELETE           = 0x0003
    FOF_ALLOWUNDO       = 0x0040
    FOF_NOCONFIRMATION  = 0x0010
)
 
type SHFILEOPSTRUCT struct {
    hwnd    syscall.Handle
    wFunc   uint32
    pFrom   *uint16
    pTo     *uint16
    fFlags  uint16
    fAnyOps bool
    hNameMappings uintptr
    lpszProgressTitle *uint16
}
 
func moveToRecycleBin(filePath string) error {
    shFileOp := &SHFILEOPSTRUCT{
        wFunc:  FO_DELETE,
        pFrom:  syscall.StringToUTF16Ptr(filePath + "\x00"),
        fFlags: FOF_ALLOWUNDO | FOF_NOCONFIRMATION,
    }
 
    shfileopProc, err := syscall.LoadDLL("shell32.dll").FindProc("SHFileOperationW")
    if err != nil {
        return err
    }
 
    ret, _, _ := shfileopProc.Call(uintptr(unsafe.Pointer(shFileOp)))
    if ret != 0 {
        return fmt.Errorf("SHFileOperationW failed: return value %d", ret)
    }
 
    return nil
}
 
func main() {
    err := moveToRecycleBin("C:\\path\\to\\your\\file.txt") // 替换为要删除的文件路径
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(1)
    }
}

代码解释

1.常量定义:定义了一些需要用到的常量,比如FO_DELETE(用于删除操作)和FOF_ALLOWUNDO(允许撤销)。

2.SHFILEOPSTRUCT结构体:这个结构体用于SHFileOperation函数的参数,包含了操作类型、源文件路径、目标文件路径(在这个例子中不使用),以及其他标志。

3.moveToRecycleBin函数:

  • 将文件路径转换为以空字符结尾的UTF-16字符串。
  • 加载shell32.dll动态链接库并查找SHFileOperationW函数。
  • 调用SHFileOperationW函数来执行删除操作。如果返回值不为0,则表示操作失败。

4.错误处理:如果加载DLL或查找函数失败,或者函数执行返回错误,函数会返回相应的错误。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开支付宝扫一扫,即可进行扫码打赏哦,您的支持,是我继续创作的动力。
点赞 (0)

中和威客保留所有权利,未经本站书面许可不得转载本站内容!文中观点不代表本站立场!

中和威客 嵌入式 C++ Go语言实现将windows和linux文件删除至回收站 https://www.izhwk.com/archives/232

C++ Go语言实现将windows和linux文件删除至回收站
上一篇:

已经没有上一篇了!

常见问题
  • 您需要注册成为本站会员,然后再通过会员中心的升级VIP功能,方可成为本站的VIP会员。
查看详情
  • 首先您需要注册成为本站会员,然后到会员中心充值,充值后支付对应资源的查看金额即可查看付费内容。
查看详情

相关文章

评论
暂无评论
C++ Go语言实现将windows和linux文件删除至回收站-海报

分享本文封面