管理

C#怎样判断回收站是否为空

Posted on 2022-10-21 10:48  lzhdim  阅读(15)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

///C# 判断回收站是否为空
///作者:三角猫
namespace 操作回收站
{
    class Program
    {
        [StructLayout(LayoutKind.Explicit, Size = 20, Pack=4)]
        public struct SHQUERYRBINFO
        {
            [FieldOffset(0)]
            public int cbSize;
            [FieldOffset(4)]
            public long i64Size;
            [FieldOffset(12)]
            public long i64NumItems;
        }

        [DllImport("shell32.dll")]
        static extern int SHQueryRecycleBin(string pszRootPath, ref SHQUERYRBINFO pSHQueryRBInfo);

        static void Main(string[] args)
        {
            if (IsRecyleBinEmpty())
                Console.Write("回收站为空");
            else
                Console.Write("回收站非空");

            Console.ReadKey();
        }

        static Boolean IsRecyleBinEmpty()
        {
            SHQUERYRBINFO sqrbi = new SHQUERYRBINFO();
            sqrbi.cbSize = Marshal.SizeOf(typeof(SHQUERYRBINFO));
            int hResult = SHQueryRecycleBin(string.Empty, ref sqrbi);
            if (hResult == 0)
            {
                return(sqrbi.i64NumItems <= 0);
            }

            throw new Exception("查询回收站发生错误!");
        }
    }
}

 

Copyright © 2000-2022 Lzhdim Technology Software All Rights Reserved