清除回收站编程使用PowerShell [英] Clear Recycle Bin programmatically with Powershell

查看:371
本文介绍了清除回收站编程使用PowerShell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个最难的事情我不得不做的是访问的Windows API使用PowerShell。我想用在Shell32.dll中的API来清除回收站。这样做有它的其他方式,但它们通常绕过正常的Windows进程,在这种情况下,我想这样做的正确的方式。

One of the hardest things I've had to do is access Windows APIs with PowerShell. I want to erase the Recycle Bin using the API in Shell32.dll. There are other ways of doing it, but they are typically bypassing the normal Windows processes and in this case, I want to do it the "right" way.

推荐答案

在几个小时后,我来到了这一点。

After a few hours, I came up with this.

$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()

这篇关于清除回收站编程使用PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆