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("查询回收站发生错误!"); } } }
Austin Liu 刘恒辉
Project Manager and Software Designer E-Mail:lzhdim@163.com Blog:https://lzhdim.cnblogs.com 欢迎收藏和转载此博客中的博文,但是请注明出处,给笔者一个与大家交流的空间。谢谢大家。 |