如何在不使用Shell32的情况下使用C#中的API计算回收站中的文件 [英] How to count Files in the Recycle Bin with API in C# without using Shell32

查看:91
本文介绍了如何在不使用Shell32的情况下使用C#中的API计算回收站中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我只想检查回收站是否为空。我找到了很多代码但没有工作。我想用API做这个,我不想引用Shell命名空间。使用Shell命名空间,我现在已经找到了一个有效的解决方案,我寻求API解决方案。我使用的这段代码失败了,我不明白为什么这对我不起作用:



 StructLayout(LayoutKind.Explicit,Size =  20 ,Pack =  4 )] 
public struct SHQUERYRBINFO
{
[FieldOffset( 0 )]
public int cbSize;
[FieldOffset( 4 )]
public i64Size;
[FieldOffset( 12 )]
public i64NumItems;
}

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

public static int GetCount()
{
SHQUERYRBINFO sqrbi = new SHQUERYRBINFO();
sqrbi.cbSize = Marshal.SizeOf( typeof (SHQUERYRBINFO));
int hResult = SHQueryRecycleBin( String .Empty, ref sqrbi);
if (hResult == 0
{
return int )sqrbi.i64NumItems;
}
return 0 ;
}





函数调用本身已经失败,返回值不为零。

I在安装了所有Windows更新的实际Intel机器上使用Windows 7 64位专业版和VS 2008和NET 2.0。可能64位是问题吗?

解决方案

您是否尝试使用Directory.GetFiles(@C:\


回收.BIN)?

Basically I want just to check if the Recycle Bin is empty or not. I have found plenty of codes but none work. I want to do this with the API, I do not want to reference the Shell Namespace. With Shell Namespace I got already a working solution now I seek for the API solution. This code I use and it fails, I cannot see why this does not work for me:

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);

        public static int GetCount()
        {
            SHQUERYRBINFO sqrbi = new SHQUERYRBINFO();
            sqrbi.cbSize = Marshal.SizeOf(typeof(SHQUERYRBINFO));
            int hResult = SHQueryRecycleBin(String.Empty, ref sqrbi);
            if (hResult == 0)
            {
                return (int)sqrbi.i64NumItems;
            }
            return 0;
        }



The return value is not zero the function call itself fails already.
I use Windows 7 64 Bit Pro and VS 2008 with NET 2.0 on an actual Intel Machine with all Windows Updates installed. Maybe 64 Bit is the issue?

解决方案

Have you tried with Directory.GetFiles(@"C:\


Recycle.bin")?


这篇关于如何在不使用Shell32的情况下使用C#中的API计算回收站中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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