gpt4 book ai didi

c# - 使用 Powershell 以编程方式清除回收站

转载 作者:太空狗 更新时间:2023-10-30 01:36:12 24 4
gpt4 key购买 nike

我必须做的最困难的事情之一是使用 PowerShell 访问 Windows API。我想使用 Shell32.dll 中的 API 清除回收站。还有其他方法可以做到这一点,但它们通常会绕过正常的 Windows 进程,在这种情况下,我想以“正确”的方式进行。

最佳答案

几个小时后,我想到了这个。

$TypeDefinition=@"
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace shell32 {

//Put all the variables required for the DLLImports here
enum RecycleFlags : uint { SHERB_NOCONFIRMATION = 0x00000001, SHERB_NOPROGRESSUI = 0x00000002, SHERB_NOSOUND = 0x00000004 }

public static class RecycleBin {
[DllImport("Shell32.dll",CharSet=CharSet.Unicode)]
internal static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);
}

public class ShellWrapper : IDisposable {

// Creates a new wrapper for the local machine
public ShellWrapper() { }

// Disposes of this wrapper
public void Dispose() {
GC.SuppressFinalize(this);
}

//Put public function here
public uint Empty() {
uint ret = RecycleBin.SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlags.SHERB_NOCONFIRMATION | RecycleFlags.SHERB_NOPROGRESSUI | RecycleFlags.SHERB_NOSOUND);
return ret;
}

// Occurs on destruction of the Wrapper
~ShellWrapper() {
Dispose();
}

} //Wrapper class
}
"@
Add-Type -TypeDefinition $TypeDefinition -PassThru | out-null
$RecycleBin=new-object Shell32.ShellWrapper

$RecycleBin.Empty()

关于c# - 使用 Powershell 以编程方式清除回收站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22871595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com