首页 新闻 会员 周边 捐助

C#的回收站64位程序运行问题

0
悬赏园豆:10 [已解决问题] 解决于 2024-07-18 15:04

这边有个获取回收站大小和文件个数的函数,但是目前只在32位目标平台才能用,请问如何更改让其在64位平台也能够使用???

 

/// <summary>
/// 回收站信息
/// </summary>
public class RecycleBinInfo
{
    public long NumItems { get; set; }
    public long Size { get; set; }
}


/// <summary>
/// 获取回收站大小和文件个数
/// </summary>
/// <param name="RBsize"></param>
/// <param name="RBNumItems"></param>
/// <returns></returns>
public static RecycleBinInfo GetRecycleBinInfo()
{
    SHQUERYRBINFO info = new SHQUERYRBINFO();
    info.cbSize = (uint)Marshal.SizeOf(info);

    if (SHQueryRecycleBin(null, ref info) == 0)
    {
        return new RecycleBinInfo() { Size = info.i64Size, NumItems = info.i64OriginalSize };
    }

    return new RecycleBinInfo() { Size = 0, NumItems = 0 };
}

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

[StructLayout(LayoutKind.Explicit, Size = 40)]
private struct SHQUERYRBINFO
{
    [FieldOffset(0)] public uint cbSize;
    [FieldOffset(4)] public uint i64Size;
    [FieldOffset(12)] public uint i64OriginalSize;
    [FieldOffset(16)] public uint dwItems;
    [FieldOffset(20)] public uint uDriveType;
    [FieldOffset(24)] public uint uDriveSerialNumber;
    [FieldOffset(28)] public uint uFileFlags;
    [FieldOffset(32)] public uint uFileAttributes;
    [FieldOffset(36)] public uint dwVolumeFlags;
}
C#
lzhdim的主页 lzhdim | 小虾三级 | 园豆:650
提问于:2024-07-18 10:40
< >
分享
最佳答案
1

uint 换成 ulong

            [FieldOffset(4)] public ulong i64Size;
            [FieldOffset(12)] public ulong i64OriginalSize;
收获园豆:10
复制粘贴机器人 | 小虾三级 |园豆:720 | 2024-07-18 14:53

谢谢,搞定了。。。

 

[StructLayout(LayoutKind.Explicit)]
private struct SHQUERYRBINFO
{
    [FieldOffset(0)] public ulong cbSize;
    [FieldOffset(8)] public ulong i64Size;
    [FieldOffset(16)] public ulong i64OriginalSize;
}

lzhdim | 园豆:650 (小虾三级) | 2024-07-18 15:04
其他回答(1)
0

你用这个调试一下看看. 我看了下,应该是cbSize的大小在64和86上不一样

        [StructLayout(LayoutKind.Explicit, Size = 32)]
        private struct SHQUERYRBINFO
        {
            [FieldOffset(0)] public uint cbSize;
            [FieldOffset(4)] public uint _1;
            [FieldOffset(8)] public uint _2;
            [FieldOffset(12)] public uint _3;
            [FieldOffset(16)] public uint _4;
            [FieldOffset(20)] public uint _5;
            [FieldOffset(24)] public uint _6;
            [FieldOffset(28)] public uint _7;
            [FieldOffset(32)] public uint _8;
        }
www378660084 | 园豆:1248 (小虾三级) | 2024-07-18 15:32

谢谢,已经搞定了啊。。。

 

32为是4个字节,偏移量是4;而64位是8个字节,偏移量是8;我上面已经贴出代码了啊。声明那个。。。

支持(1) 反对(0) lzhdim | 园豆:650 (小虾三级) | 2024-07-21 22:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册